Operating System - HP-UX
1833323 Members
2833 Online
110051 Solutions
New Discussion

Problem with the test command

 
SOLVED
Go to solution
Radhe Webster
Occasional Advisor

Problem with the test command

Here is a small test example:
if [ -z "${TEMPDIR}" ]; then
TEMPDIR=/tmp; export TEMPDIR
fi
~

This runs perfectly fine if run normally but to try and add this to a script and source it, i.e:
. /tmp/test generates an error.

sh: TEMPDIR: Parameter not set.

Why does this not work when sourcing the file?
Thanks.
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Problem with the test command

Shalom,

It would be marginally helpful to know what shell you are using.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Problem with the test command

Hi:

Stictly speaking, the '-z' test tests to see if the string is of size zero. If the string has never been defined, even to an empty string, the shell will report that the parameter isn't set.

An easy, very succinct way to do what you want is:

# export TEMPDIR=${TEMPDIR:=/tmp}

...now, if the parameter is not set or it is null, then it set it to "/tmp" (or whatever you choose.

Regards!

...JRF...
Radhe Webster
Occasional Advisor

Re: Problem with the test command

I'm using HP-UX 11.23 POSIX shell (/sbin/sh).

And i'm not really able to test it like you've specified. As part of the script, I need to know if the parameter has been set from an environmental variable or not (that is not part of the script).

Like I said it works when calling it normally, ie, (directly fromt the command line)but fails when sourcing it. Is there a reason for this?
Bill Hassell
Honored Contributor
Solution

Re: Problem with the test command

There is a BIG difference between an undefined (never set) variable and one that has a null string as a value. Your script works differently due to the setting of set -u or set +u. I always recommend set -u which means you will get the error: Parameter not set. This is a very good thing because using a variable that was never defined can lead to big mistakes later on. The most common reason for undefined variables is a spelling error (like TEMPDIR versus TMPDIR).

So James's solution is the correct one. You leave set -u in effect to catch errors but use the special shell construct:

export TEMPDIR=${TEMPDIR:=/tmp}

This handles two very different conditions: TEMPDIR was never defined or given a value, and TEMPDIR="" which has TEMPDIR defined but to a null value. This is one of the best techniques to handle undefined variables without turning on set +u.

Oh, and just to clarify, you no longer need to test the variable with -z because it will always have a value. Try this:

set -u
unset TEMPDIR
if [ -z "${TEMPDIR}" ]; then
TEMPDIR=/tmp; export TEMPDIR
fi

and you'll see the error message.

set -u
TEMPDIR=""
if [ -z "${TEMPDIR}" ]; then
TEMPDIR=/tmp; export TEMPDIR
fi

Now there is no error and your test will be successful. But use this technique:

set -u
unset TEMPDIR
export TEMPDIR=${TEMPDIR:=/tmp}
echo $TEMPDIR

TEMPDIR=""
export TEMPDIR=${TEMPDIR:=/tmp}
echo $TEMPDIR

TEMPDIR=/var/tmp
export TEMPDIR=${TEMPDIR:=/tmp}
echo $TEMPDIR

Now you will see that all 3 conditions are handled correctly.


Bill Hassell, sysadmin