Open System Services Shell and Utilities Reference Manual (G06.27+, H06.04+)
User Commands (t - u) test(1)
string1 != string2
TRUE if string1 and string2 are not identical.
string1 TRUE if string1 is not the null string.
number1 -eq number2
TRUE if the integers number1 and number2 are algebraically equal. Any of the
comparisons -ne (not equal to), -gt (greater than), -ge (greater than or equal to),
-lt (less than), and -le (less than or equal to) can be used in place of -eq.
The listed functions can be combined with the following operators:
! Unary negation operator.
-a Binary AND operator.
-o Binary OR operator (-a has higher precedence than -o).
\( expression \) Parentheses for grouping. There must be a space after
\( and before \).
EXAMPLES
1. To test whether a file exists and is not empty, enter:
if test -s "$1"
then
echo $1 does not exist or is empty.
fi
If the file specified by the first positional parameter to the shell procedure does not exist,
this example displays an error message. If $1 exists, this example displays nothing.
Note that there must be a space between -s and the filename.
The double quotes around $1 ensure that the test will work properly even if the value of
$1 is the empty string. If the double quotes are omitted and $1 is the empty string, test
displays the error message test: parameter expected.
2. To do a complex comparison, enter:
if [ $# -lt 2 -o ! -s "$1" ]
then
exit
fi
If the shell procedure was given fewer than two positional parameters or if the file
specified by $1 does not exist or is empty, then this example exits the shell procedure.
The special shell variable $# represents the number of positional parameters entered on
the command line that started this shell procedure.
Note that there must be a space before and after the [ character and before the ] charac-
ter. There must also be a space before the -lt flag and before the -s flag.
EXIT VALUES
The test command evaluates expression and, if its value is TRUE, returns an exit value of 0
(zero); otherwise, it returns a value of 1 (FALSE); the test command also returns an exit value of
1 if there are no arguments.
527188-004 Hewlett-Packard Company 9−11