- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How do I test to see if a variable is set in a...
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
11-14-2005 03:25 AM
11-14-2005 03:25 AM
TIA
Rex
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2005 03:32 AM
11-14-2005 03:32 AM
SolutionYou *should* be able to use the -z or -s switch to test to verify string length:
[[ -z "${var}" ]] && echo "string is zero length"
[[ -s "${var}" ]] && echo "String has a length"
I've had mixed results with that in the past, however. What I normally do is:
[[ "${var}x" = "x" ]] && echo "\${var} not set" || echo "\${var} is set"
HTH;
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2005 03:37 AM
11-14-2005 03:37 AM
Re: How do I test to see if a variable is set in a script?
I assume that you mean "in a shell script". If so, you can test with "-z". For instance:
#/usr/bin/sh
[ -z "${X}" ] && echo Empty!
...echo appropriately if the variable is empty.
Be sure to enclose the variable in double quotes to avoid syntatic failures at runtime.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2005 03:39 AM
11-14-2005 03:39 AM
Re: How do I test to see if a variable is set in a script?
if set | grep -q "^x=" ; then echo "x has been initialized" ; fi
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2005 04:24 AM
11-14-2005 04:24 AM
Re: How do I test to see if a variable is set in a script?
Rodney, I saw your suggestion and had one of those "duh" moments where I realised I was too busy looking for the trees to see the forest. Yours was the suggestion I decided to go with.
Thanks all for your quick responses.
Rex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2005 04:25 AM
11-14-2005 04:25 AM