Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to use $variable in conditional sentences?

Shell Programming and Scripting


Tags
make, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-17-2017   -   Original Discussion by icvallejo
icvallejo icvallejo is offline
Registered User
 
Join Date: May 2017
Last Activity: 19 May 2017, 4:32 AM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
How to use $variable in conditional sentences?

Hello all

I am doing a Makefile but I can't return the value of $var to use it in conditional sentences:


Code:
#!/bin/sh

GO=$(shell) go
GOPATH=$(GO) env GOPATH

make:
    @$(GOPATH)
    @if [ ! -d "$(GOPATH)/bin" ]; then mkdir -p "$(GOPATH)/bin" ; fi

When I type "make", @$GOPATH returns /home/icvallejo/go so it's ok, but after, I don't know how to use it (I can't do it) with 'if' condition, make returns this:


Code:
if [ ! -d " go env GOPATH/bin" ] ; then mkdir -p " go env GOPATH/bin" ; fi

Can you help me please?

Last edited by rbatte1; 05-17-2017 at 05:51 AM..
Sponsored Links
    #2  
Old Unix and Linux 05-17-2017   -   Original Discussion by icvallejo
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 24 November 2017, 3:49 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,637
Thanks: 107
Thanked 1,604 Times in 1,181 Posts
I think you mix up two different things: shell scripts and makefiles.

Makefiles (more precisely: the make-utility) work rule-based, so you don't need explicit conditionals - everything is a conditional anyway.

make works like that: you define so-called "dependencies" between files: i.e. you have three object files where each depends on a single source file. Whenever one of the source file changes the corresponding object file has to be generated anew. This is done by executing the code in the rule-definition. For every dependency you can create a rule, but usually you create rules for groups of dependencies: whenever ".c" (the source) changes, the corresponding ".obj" (the object) has to be generated and the rule for this is to call the compiler to compile exactly the one source-file. For this there are "make-variables" like "$@", "$<", etc., which are filled with the name(s) of the files involved in the rule. See the man-page of make for details.

You can also create cascades of these rules: you base .obj-files on .c-files and you base executables on the .obj-files. So, when a source file changes, the corresponding object is generated and in turn this leads to the executable being generated too (by calling the linker to link all the objects to the executable.

You might want to read this little introduction i once wrote.

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
icvallejo (05-17-2017)
Sponsored Links
    #3  
Old Unix and Linux 05-17-2017   -   Original Discussion by icvallejo
icvallejo icvallejo is offline
Registered User
 
Join Date: May 2017
Last Activity: 19 May 2017, 4:32 AM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you @bakunin, now I understand the difference but it's funny because if I type gmake it works... lol
    #4  
Old Unix and Linux 05-17-2017   -   Original Discussion by icvallejo
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 November 2017, 4:41 PM EST
Location: Saskatchewan
Posts: 22,523
Thanks: 1,154
Thanked 4,275 Times in 3,948 Posts
I have no idea what you're trying to do. What is "go"? What are you actually trying to accomplish?

If this "go" command loads variables or changes the current directory, it won't work, makefiles do not work that way, they are not shell scripts. The paths and variables should be set up before you run make, not after.
Sponsored Links
    #5  
Old Unix and Linux 05-18-2017   -   Original Discussion by icvallejo
icvallejo icvallejo is offline
Registered User
 
Join Date: May 2017
Last Activity: 19 May 2017, 4:32 AM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
I have no idea what you're trying to do. What is "go"? What are you actually trying to accomplish?

If this "go" command loads variables or changes the current directory, it won't work, makefiles do not work that way, they are not shell scripts. The paths and variables should be set up before you run make, not after.
I'm sorry, I didn't explain myself correctly. go is to execute go (golang) commands.

But now it works typing gmake...
Sponsored Links
    #6  
Old Unix and Linux 05-18-2017   -   Original Discussion by icvallejo
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 November 2017, 4:41 PM EST
Location: Saskatchewan
Posts: 22,523
Thanks: 1,154
Thanked 4,275 Times in 3,948 Posts
It occurs to me your makefile is a little overcomplicated.

instead of if [ ! -d ... ] ; then mkdir -p ; fi just do mkdir -p $(GOPATH)/bin that being what the -p flag is for.

Or add these lines to the end:

Code:
$(GOPATH)/bin:
        mkdir -p $(GOPATH)/bin

No need to dump the logic into shell when makefile can handle it natively.

Of course, then you should add it to your make: line so make knows to 'build' that first: make:$(GOPATH)/bin
The Following User Says Thank You to Corona688 For This Useful Post:
icvallejo (05-19-2017)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk conditional expression to compare field number and variable value goodbenito Shell Programming and Scripting 1 01-27-2011 03:39 PM
set variable using 'ls' with an 'or' conditional statement goodbenito Shell Programming and Scripting 5 01-19-2011 02:17 AM
Conditional Compilation based on Environmental Variable in Unix BrighterLater Programming 2 05-07-2009 01:07 PM
How to filter sentences?? vanitham UNIX for Dummies Questions & Answers 3 02-10-2009 02:09 AM
comparing sentences vanitham Shell Programming and Scripting 4 10-20-2008 02:06 AM



All times are GMT -4. The time now is 09:04 AM.