Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Check if a variable has a value assigned?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 05-03-2009
garskoci garskoci is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 8 October 2014, 1:03 PM EDT
Location: Chicago, IL
Posts: 52
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 Unix and Linux 05-03-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 25 August 2015, 1:58 PM EDT
Location: H3X
Posts: 2,152
Thanks: 10
Thanked 119 Times in 112 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 Unix and Linux 05-03-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 27 July 2015, 12:41 PM EDT
Location: Toronto, Canada
Posts: 2,896
Thanks: 0
Thanked 131 Times in 116 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 Linux or Unix Question

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
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 06:51 AM.