- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Problem with the test command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 08:53 AM
08-20-2006 08:53 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 08:55 AM
08-20-2006 08:55 AM
Re: Problem with the test command
It would be marginally helpful to know what shell you are using.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 09:14 AM
08-20-2006 09:14 AM
Re: Problem with the test command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 09:42 AM
08-20-2006 09:42 AM
Re: Problem with the test command
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 12:27 PM
08-20-2006 12:27 PM
SolutionSo 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