- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: test command gives no output (true or false)
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
04-27-2001 11:29 AM
04-27-2001 11:29 AM
if [ -n part ]
then command
echo "true"
else command
echo "false"
The oputput to the screen from the echo commands always shows that it calls the test false, even if part is blank or not.
I put it straight into a prompt:
example=bob
test -n "$example"
and I don't get a 0 or a 1, it just gives me a new prompt. Using:
test 7=7
does the same thing. Just typing test (no arguments) also does the same thing. What am I doing wrong? Why won't the test command give me any output? Help, as always, is greatly appreciated. Thnanks!
Mark
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 11:36 AM
04-27-2001 11:36 AM
Re: test command gives no output (true or false)
/bin/ksh
Prashun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 11:40 AM
04-27-2001 11:40 AM
Re: test command gives no output (true or false)
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 11:54 AM
04-27-2001 11:54 AM
Re: test command gives no output (true or false)
test doesn't not generate any output. What it does is return an exit status. 0 is true and non-zero is false. What you need to be looking at is the return code of your test, i.e. $?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 12:03 PM
04-27-2001 12:03 PM
Re: test command gives no output (true or false)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 12:16 PM
04-27-2001 12:16 PM
SolutionA couple of comments.
Get in the habit of putting quotation marks around your variables when you do 'test's:
if [ -z "X" ]
then
echo "empty variable"
else
echo "the value is $X"
fi
When evaluating an expression, 'test' returns a zero (true) exit status if the expression is "true"; otherwise, a nonzero (false) exit status. You can echo this to stdout by doing:
# echo $?
...as:
# X=
# [ -z "$X" ];echo $?
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 12:23 PM
04-27-2001 12:23 PM
Re: test command gives no output (true or false)
I forgot to add -- if you want to capture an exit status (as for instance from a 'test') and reference it later, all you need to is this:
# [ -z "$X" ]
# RC=$?
# echo "Ready to see the ReturnValue?"
# read anything
# echo "The test returned $RC"
That is, assign the variable $? to one of your choice immediately after the statement you're interesting in interrogating.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 01:03 PM
04-27-2001 01:03 PM
Re: test command gives no output (true or false)
bob=seven
test -n "$bob" ( I also tried "bob" )
echo $? gives an output of 0
How can the variable be assigned a value but still have a zero length? (using the -z option is true) Echo "$bob" gives an output of seven, so it should be filled. This doesn't make any sense to me, but maybe I have it all screwed up in my head. I don't know. Thanks.
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2001 07:01 PM
04-27-2001 07:01 PM
Re: test command gives no output (true or false)
If you are running HP-UX 10.xx or higher then you are not using the Bourne shell, it's the POSIX shell. /usr/bin/sh is the POSIX shell and is virtually identical to ksh. Bourne shell is located in /usr/old/bin if you really want it. Also note that there is no /bin directory--hasn't been since 10.xx was released about 7 years ago. The /bin directory is /usr/bin (just like Solaris) but for old code and scripts, a symbolic link is present (see man tlinstall).
-----------
Always start every script with the desired interpreter. The first line of every script should be:
#!/usr/bin/sh
(or /usr/bin/ksh or whatever the script uses).
----------
A variable can be undefined or defined but with no value (null), or defined with a value which can be a zero length string or a simple string/value. Each is a defined state and testing for each state can be tricky. Here are examples:
X=
which defines the variable with no value
X=""
which defines X with a zero length value
X="Y"
which defines X with a value of "Y"
I don't know of a way to see the difference between X= or X="" or X='' which are all null or zero length values.
I always code scripts with set -u which means undefined variables will cause an error exit when tested or used on the right side of an assignment. Therefore, to test for an undefined variable I must assign a default value if it isn't already defined like this:
UNSET=IamNOTset
DT=${DT:-$UNSET}
VUE=${VUE:-$UNSET}
TERM=${TERM:-$UNSET}
DISPLAY=${DISPLAY:-$UNSET}
SESSIONTYPE=${SESSIONTYPE:-$UNSET}
To see if the variable is not defined, just see if the variable is equal to "$UNSET" Once tested, you can restore the original state with something like this:
[ $DT = $UNSET ] && unset DT
[ $VUE = $UNSET ] && unset VUE
[ $TERM = $UNSET ] && unset TERM
[ $DISPLAY = $UNSET ] && unset DISPLAY
[ $SESSIONTYPE = $UNSET ] && unset SESSIONTYPE
To test for zero length, use test -n or -z depending on which true exit you want.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2001 05:56 PM
04-29-2001 05:56 PM
Re: test command gives no output (true or false)
returns 0 (true) if the sting is not empty, and 1 (false)
if the string is empty. Unbound variables may give
different results depending on quoting.
You can force the result for unbound variables with the
syntax test -n ${var:-''} or test -n "${var:-''}" even if
set -u has been done. You may need this when using
variables from the environment as they may not be set.
Syntax to check for an undefined variable is:
test ${HOME:-'x'} = ${HOME:-''}
You can force use of the standalone test command with
/usr/bin/test instead of test or [ ]. I always rely on the
shell built-in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2001 07:21 AM
04-30-2001 07:21 AM
Re: test command gives no output (true or false)
Mark