Operating System - HP-UX
1827894 Members
1547 Online
109969 Solutions
New Discussion

Re: ksh test of not existing variable

 
Gerhard Rottmeier
New Member

ksh test of not existing variable

hi,

need to check if a variable is not set:
"X${VAR}X" = "XX" -> true if not set or empty

any hints

regards
markus
15 REPLIES 15
Dietmar Konermann
Honored Contributor

Re: ksh test of not existing variable

set +u
if [[ -z "$VAR" ]]; then
echo empty
fi
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Muthukumar_5
Honored Contributor

Re: ksh test of not existing variable

We can check variable is set or not as,

if [[ $( set | grep -q '^variable' ) -eq 0 ]]
then
echo "Varible is set $(set | grep '^variable')"
else
echo "variable is not set"
fi

IF you use -z then,

If a variable called test is not define then,

it will give the same error as "ksh: variable is not set there"

HTH.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: ksh test of not existing variable

We can do another check as,

echo $variable 2>/tmp/testfile.log

if [[ -s /tmp/testfile.log ]]
then
echo "$variable is not set"
else
echo "$variable is set"
fi

HTH.
Easy to suggest when don't know about the problem!
Franky_1
Respected Contributor

Re: ksh test of not existing variable

Hi,

if test "X${VAR}X" = "XX"
then echo "Var not set"
else echo "Var is set"
fi

Regards

Franky
Don't worry be happy
Dietmar Konermann
Honored Contributor

Re: ksh test of not existing variable

Muthukumar,

*please* do not use external commands like grep(1) or even external files in scripts just for testing a shell variable. OK, HP may earn money by selling new/faster hardware due to bad performing script programming... :-). Check the sh-posix(1) manpage for the meaning of "set +u", which I used in the example.

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Bharat Katkar
Honored Contributor

Re: ksh test of not existing variable

Hi Markus,
it can be done this way also:

#!/usr/bin/ksh
echo $VAR
RESULT=`echo $?`
if [ $RESULT -eq 0 ]
then
echo "Paramter is set"
else
echo "Parameter is not set"
fi



You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: ksh test of not existing variable

hai konermann,

Thanks for your great guidance there.

But that fact is, i tried to show some variation there on replies there. :( I will avoid using external files, grep checks there.

Keep your feedback(s).

Best Regars,
Muthukumar.
Easy to suggest when don't know about the problem!
Gerhard Rottmeier
New Member

Re: ksh test of not existing variable

Hi,

thanks to all

will use:
T=`echo X${AB}X`
if [[ "$T" = "XX" ]]
then
# not set
else
# set
fi
Franky_1
Respected Contributor

Re: ksh test of not existing variable

Hi,

great seems your problem is solved - so please don't forget assigning points here

Regards

Franky
Don't worry be happy
Bharat Katkar
Honored Contributor

Re: ksh test of not existing variable

Hi markus,
It seems you new to forum.
See the link below on points assignments:
http://forums1.itrc.hp.com/service/forums/helptips.do?admit=716493758+1085211538437+28353475#33
Happy forumming.
Regards,

You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: ksh test of not existing variable

Hai,

It will do error message there as "var not set" for your try. So to use as,

#!/usr/bin/ksh
set +u

if [[ "$(echo XX${AB}X)" = "XX" ]]
then
# not set
else
# set
fi

set -u
#######

so that messages will not be coming on because of not-defining variable there.

HTH.
Easy to suggest when don't know about the problem!
curt larson_1
Honored Contributor

Re: ksh test of not existing variable

how about:

x="${x:-unset}"
if [[ "$x" = "unset" ]] ;then
print "the variable x was unset or null"
fi
Jov
Honored Contributor

Re: ksh test of not existing variable

Hi All,

Is it just me or some of the above is more work than required?

I just use:

if [ "X${VAR}" = "X" ]; then
# $VAR is not set!
...
...
else
# $VAR is set!
...
...
fi

If you want to flip the two action block use != in the condition.

Using [[ ]] is better, but its only for "ksh" and if you need to make your scripts compatible with other *nix, the stick to "sh".


Cheers

Jov
Bill Hassell
Honored Contributor

Re: ksh test of not existing variable

ksh and the 'better' ksh, HP's POSIX shell, even BASH are all POSIX compliant shells so the [[ ]] construct is quite portable. Not very many sysadmins use the Bourne shell (/usr/old/bin/sh in HP-UX) as it is so limited.

set +u is one way to allo testing for unset variables but set +u (for the entire script) introduces a very dangerous feature: unset variables are not syntaxed and may be used as null values. Conside the following:

MYTMPDIR=tmp/bill
rm -rf /var/$TMPDIR

If the aboive script is run by root, the entire /var directory will be removed - requiring MAJOR repair! The reason is that the rm command sees: rm -rf /var/

Having made a similar mistake on more than 300 computers in less than 1 minute, I never run a script without an explicit: set -u which ensures that an unset variable will halt the script before any amage is done.

Now the good news is that my scripts are safer to debug but the bad news is that testing for an unset variable is mre difficult. However, using the POSIX shell construct to assign a value to unset variables gets around this quite handily:

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

Then later in the script, I can test the variable like this:

if [ "$DT" = $UNSET ]
then
...

And most useful, I can put it back to it's original state with:

[ $DT = $UNSET ] && unset DT

This is very useful in /etc/profile and .profile scripts.


Bill Hassell, sysadmin
Michael D'Aulerio
Regular Advisor

Re: ksh test of not existing variable

Gerhard,
I'm a little late on this subject. Here's a one line solution:

echo ${VAR:?parameter is not set}

It will print the value of VAR if the variable is set (or is an empty string). If not, it prints:

VAR: parameter is not set

Also:

set -o nounset
echo $VAR
/usr/bin/ksh: VAR: parameter not set
set +o unset

Mike D'.
Email: michael.n.daulerio@lmco.com