- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- If statement variable not 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
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
02-15-2007 02:01 AM
02-15-2007 02:01 AM
If I want to do a test on a variable that may or may not be set is this possible.
I can't for example do.
grep noddy /etc/hosts
if [ $noddy = " " ];then
do whatever
else
do whatever
fi
Is there anyway to do a test on just the variable being set?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 02:12 AM
02-15-2007 02:12 AM
Re: If statement variable not set
can't you just check the return code ($?), if the grep can't find, it is not 0.
Otherwise:
noddy=`grep noddy /etc/hosts`
if [ -z "$noddy" ] check for 0 length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 02:13 AM
02-15-2007 02:13 AM
Re: If statement variable not set
Hi,
like this:
#!/bin/sh
set | grep VARIABLE_NAME
if [ $? = 0 ]
then
echo $VARIABLE_NAME
else
'do that'
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 02:15 AM
02-15-2007 02:15 AM
Re: If statement variable not set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 02:48 AM
02-15-2007 02:48 AM
Re: If statement variable not set
if [[ -z $STRING ]] - check if string has zero length
if [[ -n $STRING ]] - check if string non zero length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 02:49 AM
02-15-2007 02:49 AM
Re: If statement variable not set
if [[ -z $STRING ]] - check if string has zero length
if [[ -n $STRING ]] - check if string non zero length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 03:33 AM
02-15-2007 03:33 AM
Re: If statement variable not set
The 'grep' filter will return zero (0) if there are matches; one (1) if not. Hence:
# grep local /etc/hosts > /dev/null || echo "no matches!"
...is a terse way to meet your goal.
You can also count and return the number of matches with the '-c' switch. See the manpages for more information.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 06:34 AM
02-15-2007 06:34 AM
SolutionIf your string is empty, you need to quote it:
if [ "$noddy" = "" ];then
(It appears this isn't needed in [[ ]].)
>Dino: set | grep VARIABLE_NAME
if [ $? = 0 ]
This should really be: (James' can be simplified with -q too.)
set | grep -q VARIABLE_NAME
if [ $? -eq 0 ]
("=" is for string comparisons, -eq for ints.)
Another iffy thing with uninitialized strings is when you use -u in your scripts:
#!/usr/bin/ksh -u
(To check for uninitialized shell variables.)
To fix gstonian's case:
if [[ -z ${STRING-} ]] ; then #check if string has zero length or not init
This may be an answer to your original question. You could do:
if [ "${STRING-NOTSET}" = "NOTSET" ]
And to make sure you are aware of if, if you are comparing two strings where it can start with a "-", you need this trick:
if [ X"${STRING-NOTSET}" = X"NOTSET" ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 07:23 AM
02-15-2007 07:23 AM
Re: If statement variable not set
But this brings to light your test requirement: how do you test for an unset variable? The easiest method (and still keep set -u enabled) is to assign a special value like this:
UNSET=IamNOTset
MYVAR=${MYVAR:-$UNSET}
Now if MYVAR has been defined (ie, appears on the left of = someplace) then MYVAR is unchanged. But if MYVAR was never defined, it will be assigned the contents of UNSET. You can use any unique string but the above IamNOTset works well. Now the following test will always succeed, even with set -u:
if [ "$MYVAR" = "" ]
then
echo "MYVAR is null"
fi
NOTE: There is a big difference between being unset (never defined) and being null. Assigning any value including nothing defines the variable.
Another advantage to using a common UNSET value is that you can return the variable ot it's original value, including unset:
if [ $MYVAR = $UNSET ]
then
unset MYVAR
fi
In your example, noddy is always defined so it will succeed with:
if [ "$noddy" = "" ]
With no result from grep, the if statement will look like this:
if [ "" = "" ]
which is a valid comparison. But as mentioned before, grep -c may be a better choice. For instance, there may be 0, 1, 2 or more in which each value is useful.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 07:59 AM
02-15-2007 07:59 AM
Re: If statement variable not set
if [ ${noddy:-} ]; then
echo "noddy is set to $noddy"
else
echo "noddy is unset"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2007 08:36 PM
02-15-2007 08:36 PM