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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
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; 1 Week Ago at 04:51 AM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 28 May 2017, 6:22 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,427
Thanks: 100
Thanked 1,502 Times in 1,116 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
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 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 May 2017, 4:17 PM EDT
Location: Saskatchewan
Posts: 22,183
Thanks: 1,092
Thanked 4,174 Times in 3,861 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 1 Week Ago
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 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 May 2017, 4:17 PM EDT
Location: Saskatchewan
Posts: 22,183
Thanks: 1,092
Thanked 4,174 Times in 3,861 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 (1 Week Ago)
Sponsored Links
Reply

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 02:39 PM
set variable using 'ls' with an 'or' conditional statement goodbenito Shell Programming and Scripting 5 01-19-2011 01:17 AM
Conditional Compilation based on Environmental Variable in Unix BrighterLater Programming 2 05-07-2009 12:07 PM
How to filter sentences?? vanitham UNIX for Dummies Questions & Answers 3 02-10-2009 01:09 AM
comparing sentences vanitham Shell Programming and Scripting 4 10-20-2008 01:06 AM



All times are GMT -4. The time now is 07:34 AM.