LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-13-2009, 12:23 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Checking variables in bash


Code:
#!/bin/bash
export MYVAR="apple"
if [ ! $?MYVAR ]
then 
export MYVAR="orange"
echo "MYVAR was not previously set to $MYVAR"
else
echo "MYVAR is set to $MYVAR" 
fi
The output of the above simple script is "MYVAR is set to apple", no surprise.

However if I comment out the first export statement so that MYVAR is not set I would expect the 'then' statement to execute. MYVAR would get set to "orange" and the output should be "MYVAR was not previously set to orange". Instead it outputs "MYVAR is set to ". So whether MYVAR is set or not it executes the else statement.

To work around this I can change the if statement as follows.

Code:
if [ ! "$MYVAR" ]
Taking out the '?' and wrapping double quotes around the variable works.

Shouldn't 'then' execute if MYVAR is not set in the original script?
 
Old 02-13-2009, 12:57 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
you cant comment out an if statement and expect a then statement to still work for one
or are you commenting out the if then else and fi
second of all if you comment out the export myvar=apple instead of the if, THEN you should see your then work
if [ ! "$MYVAR" ] only checks if the variable is null
try if [ if $myvar != "orange" ] instead
 
Old 02-13-2009, 03:23 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
if [ ! $?MYVAR ]
This is not a valid BASH syntax - or better - it is not what you expect to be. The notation $?varname expands to 1 if varname is set, expands to 0 if varname is not set, but only in C-shells!

To check if a variable is set or not in Bash, you have to use
Code:
if [ -z "$MYVAR" ]
where the -z test means "the length of string is zero".

When you use the $?MYVAR notation in Bash, it is interpreted as the concatenation of $? (the exit status of the previous command) plus the literal string "MYVAR". You can easily demonstrate it running the script with bash -x. This will report a trace of the actual commands executed by the shell:
Code:
$ bash -x test.sh
+ export MYVAR=apple
+ MYVAR=apple
+ '[' '!' 0MYVAR ']'
+ echo 'MYVAR is set to apple'
MYVAR is set to apple
As you can see, the string in red is the result of the expansion performed by the shell, as mentioned above. In summary, the correct Bash version of your script should be:
Code:
#!/bin/bash
#export MYVAR="apple"
if [ -z $MYVAR ]
then
  export MYVAR="orange"
  echo "MYVAR was not previously set to $MYVAR"
else
  echo "MYVAR is set to $MYVAR"
fi
 
Old 02-13-2009, 07:09 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
thanks colucix. Your detailed explanation went above and beyond the call of duty.

frieza, your misunderstanding is ...er.. understandable considering my poor explanation of the issue. I did not comment out the 'if' statement. I commented out the first 'export' statement.

Regards all,
 
Old 02-13-2009, 08:07 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
It seems the best hidden secret of shell programming, but run your script with:

Code:
sh -x myscript
It becomes immediately apparent what all variables and statements are expanded to and solves 80% of your problems.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Variables SlipDigby Programming 2 09-01-2008 05:00 PM
Bash: what happens to my variables? elinenbe Programming 5 01-30-2008 02:32 PM
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration