- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Test if variable is set
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
Discussions
Discussions
Discussions
Forums
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
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
тАО04-27-2004 09:51 PM
тАО04-27-2004 09:51 PM
What is the best way to test if a variable has been set? (ksh)
Like:
if (variable set?)
do something
fi
Thx
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2004 09:56 PM
тАО04-27-2004 09:56 PM
Re: Test if variable is set
You could test if the variable has any lenght with test :
[ -z "$var" ] && echo "length of string var is zero"
Cheers
Nicolas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2004 09:57 PM
тАО04-27-2004 09:57 PM
Re: Test if variable is set
like here:
[ring]//usr/include/sys:echo $PWD
/usr/include/sys
[ring]//usr/include/sys:echo $?
0
[ring]//usr/include/sys:echo $rest
sh: rest: Parameter not set.
[ring]//usr/include/sys:echo $?
1
[ring]//usr/include/sys:
exit status 1 returns if command exit status wasn't ok (ie parameter doesn't exist).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2004 09:59 PM
тАО04-27-2004 09:59 PM
Re: Test if variable is set
echo ${variable}
or
if [ ${variable) = ""
then
echo "null"
else
{do something)
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2004 09:59 PM
тАО04-27-2004 09:59 PM
Re: Test if variable is set
#!/bin/ksh
A1="abcd"
unset A2
A3=""
if [[ ! -z $A1 ]]; then
echo "A1 is set"
else
echo "A1 is not set"
fi
if [[ ! -z $A2 ]]; then
echo "A2 is set"
else
echo "A2 is not set"
fi
if [[ ! -z $A3 ]]; then
echo "A3 is set"
else
echo "A3 is not set"
fi
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A1 is set
A2 is not set
A3 is not set
In fact A3 is set (and empty).
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2004 10:13 PM
тАО04-27-2004 10:13 PM
Re: Test if variable is set
then
execute
else
echo .....
fi
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 12:15 AM
тАО04-28-2004 12:15 AM
Re: Test if variable is set
In my case I want to check if the LANG variable is set. Is there a difference in behavior between:
1) no LANG variable set
2) LANG variable set to nothing (empty string)
THX
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 12:29 AM
тАО04-28-2004 12:29 AM
Re: Test if variable is set
env command will list vars. so
TEST=`env | grep -q LANG`
if [ $? -ne 0 ]
then
echo "LANG is not set"
else
echo "LANG is $LANG"
fi
HTH,
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 12:41 AM
тАО04-28-2004 12:41 AM
Re: Test if variable is set
I was looking at env also, but what if env returns:
...
X=LANGUAGE
NLS_LANG=DUTCH_THE NETHERLANDS.WE8ISO8859P15
LANG=Dutch
...
How can if find only the LANG variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 12:56 AM
тАО04-28-2004 12:56 AM
Re: Test if variable is set
env | grep ^LANG the ^states the line must start with LANG
HTH,
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:07 AM
тАО04-28-2004 01:07 AM
Re: Test if variable is set
The shell deal with memory allocation on its own. Therefore, the declaration of a shell variable or the test of its existence can just take the form a value lookup. If you ask the value of a non-existent variable, the shell will tel you it's value is NULL (nothing) but it won't give you any error.
Cheers
Nicolas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:30 AM
тАО04-28-2004 01:30 AM
Solutionset -u
# Set any variables that might not be defined with a default
# value. That way, we can leave set -u and still check unset values
UNSET=IamNOTset
DT=${DT:-$UNSET}
VUE=${VUE:-$UNSET}
# now check if the variable was ever set:
if [ "$VUE" = $UNSET -o "$DT" = $UNSET ]
then
... setup for non-Xwindow stuff
else
... setup for Xwindows
fi
# In case this is a 'sourced' script,
# return the unset variables backup to
# their original state:
[ $DT = $UNSET ] && unset DT
[ $VUE = $UNSET ] && unset VUE
---
The key to handling an unset variable is to use the shell contstruct ${VAR_NAME:-something} which ensures that the variable is defined with a unique value (IamNOTset) so it can be tested later.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:39 AM
тАО04-28-2004 01:39 AM
Re: Test if variable is set
${A1:="zxy"}
will initialise A1 if not set (or is null) with "zxy"
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:46 AM
тАО04-28-2004 01:46 AM
Re: Test if variable is set
[ "A$VAR" = "A" ] && echo "$VAR is not set"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:50 AM
тАО04-28-2004 01:50 AM
Re: Test if variable is set
I think if VAR is set but empty this would return "not set"
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 01:53 AM
тАО04-28-2004 01:53 AM
Re: Test if variable is set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2004 03:47 AM
тАО04-28-2004 03:47 AM
Re: Test if variable is set
if ( env|grep -q ^LANG= ); then
unset LANG
fi
THX All for your replies!