- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: ksh test of not existing variable
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
10-06-2004 06:47 PM
10-06-2004 06:47 PM
ksh test of not existing variable
need to check if a variable is not set:
"X${VAR}X" = "XX" -> true if not set or empty
any hints
regards
markus
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 06:54 PM
10-06-2004 06:54 PM
Re: ksh test of not existing variable
if [[ -z "$VAR" ]]; then
echo empty
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 07:03 PM
10-06-2004 07:03 PM
Re: ksh test of not existing variable
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 07:10 PM
10-06-2004 07:10 PM
Re: ksh test of not existing variable
echo $variable 2>/tmp/testfile.log
if [[ -s /tmp/testfile.log ]]
then
echo "$variable is not set"
else
echo "$variable is set"
fi
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 07:37 PM
10-06-2004 07:37 PM
Re: ksh test of not existing variable
if test "X${VAR}X" = "XX"
then echo "Var not set"
else echo "Var is set"
fi
Regards
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 07:42 PM
10-06-2004 07:42 PM
Re: ksh test of not existing variable
*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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 08:11 PM
10-06-2004 08:11 PM
Re: ksh test of not existing variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 09:12 PM
10-06-2004 09:12 PM
Re: ksh test of not existing variable
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 10:22 PM
10-06-2004 10:22 PM
Re: ksh test of not existing variable
thanks to all
will use:
T=`echo X${AB}X`
if [[ "$T" = "XX" ]]
then
# not set
else
# set
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 10:49 PM
10-06-2004 10:49 PM
Re: ksh test of not existing variable
great seems your problem is solved - so please don't forget assigning points here
Regards
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 11:00 PM
10-06-2004 11:00 PM
Re: ksh test of not existing variable
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2004 11:26 PM
10-06-2004 11:26 PM
Re: ksh test of not existing variable
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2004 12:47 AM
10-07-2004 12:47 AM
Re: ksh test of not existing variable
x="${x:-unset}"
if [[ "$x" = "unset" ]] ;then
print "the variable x was unset or null"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2004 01:30 PM
10-07-2004 01:30 PM
Re: ksh test of not existing variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2004 02:06 PM
10-07-2004 02:06 PM
Re: ksh test of not existing variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:46 AM
10-14-2004 03:46 AM
Re: ksh test of not existing variable
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'.