Command Format: test expression
expression - composed of constants, variables, and
operators
Expressions will be looked at in greater detail later with some
examples. There are a few items that need to be mentioned that
apply to expressions. Expressions can contain one or more
evaluation criteria that test will evaluate. A -a that separates
two criteria is a logical AND operator. In this case, both
criteria must evaluate to true in order for test to return a value
of true. The -o is the logical OR operator. When this operator
separates two criteria, one or the other (or both) must be true for
test to return a true condition.
You can negate any criterion by preceding it with an exclamation
mark (!). Parentheses can be used to group criteria. If there
are no parentheses, the -a (logical AND operator) takes precedence
over the -o (logical OR operator). The test utility will evaluate
operators of equal precedence from left to right.
Within the expression itself, you must put special characters, such
as parentheses, in quote marks so the BourneShell will not evaluate
them but will pass them to test.
Since each element (evaluation criterion, string, or variable) in
an expression is a separate argument, each must be separated by a
space.
The test utility will work from the command line but it is more
often used in a script to test input or verify access to a file.
Another way to do the test evaluation is to surround the expression
with left and right brackets. A space character must appear after
the left bracket and before the right bracket.
test expression = [ expression ]
This criterion is true if the integer int1 has the specified
algebraic relationship to integer int2.
The valid operators (op) are:
-eq equal
-ne not equal
-gt greater than
-lt less than
-ge greater than or equal
-le less than or equal
The if statement evaluates the expression and then returns control
based on this status. The fi statement marks the end of the if,
notice that fi is if spelled backward.
The if statement executes the statements immediately following it
if the expression returns a true status. If the return status is
false, control will transfer to the statement following the fi.
Sample Session:
$cat check_args
if (test $# = 0)
then echo 'Please supply at least 1 argument'
exit
fi
echo 'Program is running'
$
This little script will check to insure that you are giving at
least one argument. If none are given it will display the error
message and exit. If one or more arguments are given it will
display "Program is running" and run the rest of the script, if
any.
Sample Session:
$check_args
Please supply at least 1 argument
$check_args xyz
Program is running
$
Command Format: if expression
then commands
else commands
fi
The else part of this structure makes the single-branch if
statement into a two-way branch. If the expression returns a true
status, the commands between the then and the else statement will
be executed. After these have been executed, control will start
again at the statement after the fi.
If the expression returns false, the commands following the else
statement will be executed.
Sample Session:
$cat test_string
number=1
numero=0001
if test $number = $numero
then echo "String values of $number and $numero are equal"
else echo "String values of $number and $numero not equal"
fi
if test $number -eq $numero
then echo "Numeric values of $number and $numero are equal"
else echo "Numeric values of $number and $numero not equal"
fi
$
Let's follow the execution of this script with tracing.
Sample Session:
$sh -x test_string
number=1
numero=0001
+ test 1 = 0001
+ echo String values of 1 and 0001 are not equal
String values of 1 and 0001 are not equal
+ test 1 -eq 0001
+ echo Numeric values of 1 and 0001 are equal
Numeric values of 1 and 0001 are equal
$chmod a+x test_string
$test_string
String values of 1 and 0001 are not equal
Numeric values of 1 and 0001 are equal
$