TEST(1,C) AIX Commands Reference TEST(1,C)
-------------------------------------------------------------------------------
test
PURPOSE
Evaluates conditional expressions.
SYNTAX
test -- expression --|
[ -- expression -- ] --|
DESCRIPTION
The test command evaluates expression and, if its value is true, returns a zero
(true) exit value; otherwise it returns a nonzero (false) exit value; test also
returns a nonzero exit value if there are no parameters.
Note: In the second form of the command, that is the one that uses square
brackets ("[ ]"), rather than the word test, the brackets must be
surrounded by blanks.
FUNCTIONS
All the functions and operators are separate parameters to test. The following
functions are used to construct expression:
-r file True if file exists and has read permission.
-w file True if file exists and has write permission.
-x file True if file exists and has execute permission. If the user
has superuser authority, test -x file evaluates to true for
any file that exists whether to not the execute permission
bits are set.
-f file True if file exists and is a regular file.
-d file True if file exists and is a directory.
-h file True if file exists and is a hidden directory.
-l file True if file exists and is a symbolic link.
-L file True if file exists and is a local file. If file resides in
a replicated file system true only if the local copy is the
primary copy.
Processed November 8, 1990 TEST(1,C) 1
TEST(1,C) AIX Commands Reference TEST(1,C)
-c file True if file exists and is a character special file.
-b file True if file exists and is a block special file.
-p file True if file exists and is a named pipe (FIFO).
-u file True if file exists and its set-user-ID bit is set.
-g file True if file exists and its set-group-ID bit is set.
-k file True if file exists and its sticky bit is set.
-s file True if file exists and has a size greater than zero.
-t [filedescr] True if the open file with file descriptor number filedescr
(1 by default) is associated with a work station device.
-z s1 True if the length of string s1 is zero.
-n s1 True if the length of the string s1 is nonzero.
s1 = s2 True if strings s1 and s2 are identical.
s1 != s2 True if strings s1 and s2 are not identical.
s1 True if s1 is not the null string.
n1 -eq n2 True if the integers n1 and n2 are algebraically equal. Any
of the comparisons -ne, -gt, -ge, -lt, and -le can be used in
place of -eq.
These 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.
EXAMPLES
1. To test whether a file exists and is not empty:
if test ! -s "$1"
then
echo $1 does not exist or is empty.
fi
Processed November 8, 1990 TEST(1,C) 2
TEST(1,C) AIX Commands Reference TEST(1,C)
If the file specified by the first positional parameter to the shell
procedure does not exist or is empty, this displays an error message. If
"$1" does exist and is not empty, it displays nothing. There must be a
space between -s and the file name.
The double quotes around "$1" ensure that the test works 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:
if [ $# -lt 2 -o ! -s "$1" ]
then
exit
fi
If the shell procedure was given fewer than two positional parameters or
the file specified by "$1" does not exist, this exits the shell procedure.
The special shell variable "$#" represents the number of positional
parameters entered on the command line that started this shell procedure.
For more details, see "Substitutions."
RELATED INFORMATION
See the following commands: "find" and "sh, Rsh."
Processed November 8, 1990 TEST(1,C) 3