if(1)
NAME
if, test − shell built-in functions to evaluate expression(s) or to make execution of actions dependent upon the evaluation of expression(s)
SYNOPSIS
sh
if condition; then action ; [ elif condition ; then action ] ... [ else action ] ; fi
test expression
[ expression ]
csh
if (expr) command
if (expr) then
commands ...
else if (expr2) then
commands ...
else
commands ...
endif
ksh
if condition ; then action [[ ; elif condition ; then action ] ... ] ; else action ; fi
test expression
[ expression ]
DESCRIPTION
sh
The condition following if is executed and, if it returns a zero exit status, the action following the first then is executed. Otherwise, the condition following elif is executed and, if its value is zero, the action following the next then is executed. Failing that, the else action is executed. If no else action or then action is executed, then the if command returns a zero exit status.
test evaluates the expression expression and, if its value is true, sets a zero (true) exit status; otherwise, a non-zero (false) exit status is set; test also sets a non-zero exit status if there are no arguments. When permissions are tested, the effective user ID of the process is used.
All operators, flags, and brackets (brackets used as shown in the second SYNOPSIS line) must be separate arguments to the test command; normally these items are separated by spaces.
Primitives:
The following primitives are used to construct expression:
−r filename True if filename exists and is readable.
−w filename True if filename exists and is writable.
−x filename True if filename exists and is executable.
−f filename True if filename exists and is a regular file. Alternatively, if /usr/bin/sh users specify /usr/ucb before /usr/bin in their PATH environment
variable, then test will return true if filename exists and is (not−a−directory). This is also the default for /usr/bin/csh users.
−d filename True if filename exists and is a directory.
−h filename True if filename exists and is a symbolic link. With all other primitives (except −L filename), the symbolic links are followed by default.
−c filename True if filename exists and is a character special file.
−b filename True if filename exists and is a block special file.
−p filename True if filename exists and is a named pipe (fifo).
−u filename True if filename exists and its set-user- ID bit is set.
−g filename True if filename exists and its set-group- ID bit is set.
−k filename True if filename exists and its sticky bit is set.
−s filename True if filename exists and has a size greater than zero.
−t [ fildes ] True if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device.
−z s1 True if the length of string s1 is zero.
−n s1 True if the length of the string s1 is non-zero.
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 may be used in place of −eq.
−L filename True if filename exists and is a symbolic link. With all other primitives (except −h filename), the symbolic links are followed by default.
Operators:
These primaries may 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. Notice also that parentheses are meaningful to the shell and, therefore, must be quoted.
The not−a−directory alternative to the −f option is a transition aid for BSD applications and may not be supported in future releases.
The −L option is a migration aid for users of other shells which have similar options and may not be supported in future releases.
If you test a file you own (the −r −w or −x tests), but the permission tested does not have the owner bit set, a non-zero (false) exit status will be returned even though the file may have the group or other bit set for that permission. The correct exit status will be set if you are super-user.
The = and != operators have a higher precedence than the −r through −n operators, and = and != always expect arguments; therefore, = and != cannot be used with the −r through −n operators.
If more than one argument follows the −r through −n operators, only the first argument is examined; the others are ignored, unless a −a or a −o is the second argument.
csh
With the one-line form of if, if the specified expression evaluates to true, the single command with arguments is executed. Variable substitution on command happens early, at the same time it does for the rest of the if command. command must be a simple command, not a pipeline, a command list, or a parenthesized command list. Note: I/O redirection occurs even if expr is false, when command is not executed (this is a bug).
With the multi-line form of if, if expr is true, commands up to the first else are executed. Otherwise, if expr2 is true, the commands between the else if and the second else are executed. Otherwise, commands between the else and the endif are executed. Any number of else if pairs are allowed, but only one else. Only one endif is needed, but it is required. The words else and endif must be the first nonwhite characters on a line. The if must appear alone on its input line or after an else.
ksh
The condition following if is executed and, if it returns an exit status of zero, the action following the first then is executed. Otherwise, the condition following elif is executed and, if its value is zero, the action following the next then is executed. Failing that, the else action is executed. If no else action or then action is executed, then the if command returns a zero exit status.
For a description of the test built-in, see the (sh) Bourne shell’s test built-in above.
EXAMPLES
In the if command examples, three conditions are tested, and if all three evaluate as true or successful, then their validities are printed to the screen. The two forms of the test built-in follow the Bourne shell’s if example.
sh
ZERO=0 ONE=1 TWO=2 ROOT=root
if [ $ONE -gt $ZERO ] &&
[ $TWO -eq 2 ] &&
[ ‘grep $ROOT /etc/passwd >&1 /dev/null‘ ]
then
echo "$ONE is greater than zero, $TWO equals 2, and root is a user-name
in the password file"
else
echo "At least one of the three test conditions is false"
fi
Examples of the test built-in:
test ‘grep $ROOT /etc/passwd >&1 /dev/null‘
echo $? /∗ test for success ∗/
[ ‘grep nosuchname /etc/passwd >&1 /dev/null‘ ]
echo $? /∗ test for failure ∗/
csh
@ ZERO = 0; @ ONE = 1; @ TWO = 2; set ROOT = root
grep $ROOT /etc/passwd >&1 /dev/null
if ( "$status" == "0" && $ONE > $ZERO && $TWO == 2 ) then
/∗ $status must be tested for immediately following grep ∗/
echo "$ONE is greater than zero, $TWO equals 2, and root is a user-name
in the password file"
endif
ksh
ZERO=0 ONE=1 TWO=2 ROOT=root
if [ $ONE -gt $ZERO ] ; [ $TWO -eq 2 ] ;
[ ‘grep $ROOT /etc/passwd >&1 /dev/null‘ ] ;
then
echo "$ONE is greater than zero, $TWO equals 2, and root is a user-name
in the password file"
fi
The Korn shell will also accept the syntax of both the if command and the test command of the Bourne shell.
When using the brackets ([]) within if commands, you must separate both ends of both brackets from other characters with a space.
SEE ALSO
csh(1), ksh(1), sh(1), test(1B)
NOTES
Both the Bourne shell, sh, and the Korn shell, ksh, can use the semicolon and the carriage return interchangeably in their syntax of the if, for, and while built-in commands.
SunOS 5.4 — Last change: 15 Apr 1994