Check if a variable has a value assigned? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Check if a variable has a value assigned?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-03-2009
sumeet sumeet is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 19 April 2012, 11:18 AM EDT
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Check if a variable has a value assigned?

Hi,

I want to check if a variable has a value assigned to it or not.

I can do following -


Code:
cat $Var > File1
if [ -s File1 ]
then
      echo "$Var has value"
else
      echo "$Var is null"
fi

But I have to check for 3 Variables and I want to wrap it up in couple of unix statements.

Any suggestion is appreciated.

Thanks
Sumeet
Sponsored Links
    #2  
Old 05-03-2009
garskoci garskoci is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 30 January 2014, 6:23 PM EST
Location: Chicago, IL
Posts: 50
Thanks: 0
Thanked 4 Times in 4 Posts
Hi Sumeet,

It may be personal preference, but I would do something more like the following when checking if a variable has contents. Then you can easily check all three rather than writing files that will probably have to be clean up. Also, you can't always be sure that you can write a file. You may not have permissions or the filesystem could be full etc.


Code:
if [ "X"$Var == "X" ]
then
      echo "\$Var in NULL"
else
      echo "\$Var has a value of $Var"
fi

Sponsored Links
    #3  
Old 05-03-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 19 April 2014, 10:39 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
man test

Code:
if [ -z $Var ]
then
      echo '$Var is null'
else
      echo '$Var has value'
fi


Last edited by danmero; 05-03-2009 at 09:30 PM..
    #4  
Old 05-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 12 April 2014, 10:59 AM EDT
Location: Toronto, Canada
Posts: 2,875
Thanks: 0
Thanked 108 Times in 101 Posts


Code:
if [ -z "${var+X}" ]
then
  echo var is unset
elif [ -z "$var" ]
then
  echo var is set but empty
else
  echo "The value of \$var is $var"
fi

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check in awk about a value of variable tal Shell Programming and Scripting 3 03-30-2009 09:27 AM
How to check if a variable contains a . gekkos Shell Programming and Scripting 3 11-27-2008 02:00 PM
Return code of command assigned to variable browndr Shell Programming and Scripting 2 08-05-2008 08:26 AM
What does $? mean when assigned to a variable? ranjita.c UNIX for Dummies Questions & Answers 3 08-24-2006 09:45 AM
How to check parameter variable? negixx Shell Programming and Scripting 4 06-10-2005 05:37 PM



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