Operating System - HP-UX
1836462 Members
2078 Online
110101 Solutions
New Discussion

If statement variable not set

 
SOLVED
Go to solution
Adam Noble
Super Advisor

If statement variable not set

Hi,

If I want to do a test on a variable that may or may not be set is this possible.

I can't for example do.

grep noddy /etc/hosts

if [ $noddy = " " ];then
do whatever
else
do whatever
fi

Is there anyway to do a test on just the variable being set?
10 REPLIES 10
Peter Godron
Honored Contributor

Re: If statement variable not set

Adam,
can't you just check the return code ($?), if the grep can't find, it is not 0.

Otherwise:
noddy=`grep noddy /etc/hosts`
if [ -z "$noddy" ] check for 0 length
Dino_4
Frequent Advisor

Re: If statement variable not set


Hi,

like this:

#!/bin/sh

set | grep VARIABLE_NAME

if [ $? = 0 ]
then
echo $VARIABLE_NAME
else
'do that'
fi
Adam Noble
Super Advisor

Re: If statement variable not set

of course I'm being dim I'll just test on the exit code
gstonian
Trusted Contributor

Re: If statement variable not set

or you can check on the variable

if [[ -z $STRING ]] - check if string has zero length
if [[ -n $STRING ]] - check if string non zero length
gstonian
Trusted Contributor

Re: If statement variable not set

or you can check on the variable if you are setting it.

if [[ -z $STRING ]] - check if string has zero length
if [[ -n $STRING ]] - check if string non zero length
James R. Ferguson
Acclaimed Contributor

Re: If statement variable not set

hi Adam:

The 'grep' filter will return zero (0) if there are matches; one (1) if not. Hence:

# grep local /etc/hosts > /dev/null || echo "no matches!"

...is a terse way to meet your goal.

You can also count and return the number of matches with the '-c' switch. See the manpages for more information.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: If statement variable not set

You need to be careful with some of the replies.
If your string is empty, you need to quote it:
if [ "$noddy" = "" ];then

(It appears this isn't needed in [[ ]].)

>Dino: set | grep VARIABLE_NAME
if [ $? = 0 ]

This should really be: (James' can be simplified with -q too.)
set | grep -q VARIABLE_NAME
if [ $? -eq 0 ]

("=" is for string comparisons, -eq for ints.)

Another iffy thing with uninitialized strings is when you use -u in your scripts:
#!/usr/bin/ksh -u
(To check for uninitialized shell variables.)

To fix gstonian's case:
if [[ -z ${STRING-} ]] ; then #check if string has zero length or not init

This may be an answer to your original question. You could do:

if [ "${STRING-NOTSET}" = "NOTSET" ]

And to make sure you are aware of if, if you are comparing two strings where it can start with a "-", you need this trick:

if [ X"${STRING-NOTSET}" = X"NOTSET" ]
Bill Hassell
Honored Contributor

Re: If statement variable not set

One of the best reliability tools is to write scripts that stop before using unset (or more likely, misspelled) variables. This could prevent the accidental remove of an entire directory's contents rather than a set of files as an example. To do this, you code set -u in front of all scripts.

But this brings to light your test requirement: how do you test for an unset variable? The easiest method (and still keep set -u enabled) is to assign a special value like this:

UNSET=IamNOTset
MYVAR=${MYVAR:-$UNSET}

Now if MYVAR has been defined (ie, appears on the left of = someplace) then MYVAR is unchanged. But if MYVAR was never defined, it will be assigned the contents of UNSET. You can use any unique string but the above IamNOTset works well. Now the following test will always succeed, even with set -u:

if [ "$MYVAR" = "" ]
then
echo "MYVAR is null"
fi

NOTE: There is a big difference between being unset (never defined) and being null. Assigning any value including nothing defines the variable.

Another advantage to using a common UNSET value is that you can return the variable ot it's original value, including unset:

if [ $MYVAR = $UNSET ]
then
unset MYVAR
fi

In your example, noddy is always defined so it will succeed with:

if [ "$noddy" = "" ]

With no result from grep, the if statement will look like this:

if [ "" = "" ]

which is a valid comparison. But as mentioned before, grep -c may be a better choice. For instance, there may be 0, 1, 2 or more in which each value is useful.


Bill Hassell, sysadmin
Sandman!
Honored Contributor

Re: If statement variable not set

Use the shell built-ins for parameter expansion i.e.

if [ ${noddy:-} ]; then
echo "noddy is set to $noddy"
else
echo "noddy is unset"
fi
Adam Noble
Super Advisor

Re: If statement variable not set

Thanks all your assistance is much appreciated and a great help