Previous | Table of Contents | Next |
Compound Expressions
So far you have seen individual expressions, but many times you need to combine expressions in order to satisfy a particular expression. When two or more expressions are combined, the result is called a compound expression.
You can create compound expressions using the test commands built in operators, or you can use the conditional execution operators, && and ||.
Also you can create a compound expression that is the negation of another expression by using the ! operator.
Table 10.4 gives a summary of these operators.
Operator | Description |
---|---|
! expr | True if expr is false. The expr can be any valid test command. |
expr1 -a expr2 | True if both expr1 and expr2 are true. |
expr1 -o expr2 | True if either expr1 or expr2 is true. |
Using the Built-in Operators The syntax for creating compound expressions using the built-in operators is
test expr1 operator expr2
or
[ expr1 operator expr2 ]
Here expr1 and expr2 are any valid test expression, and operator is either a (a as in and) or o (o as in or).
If the a operator is used, both expr1 and expr2 must be true (evaluate to 0) in order for the compound expression to be true.
If the o operator is used, either expr1 or expr2 must be true (evaluate to 0) in order for the compound expression to be true.
Using the Conditional Operators The syntax for creating compound expressions using the conditional operators is
test expr1 operator test expr1
or
[ expr1 ] operator [ expr2 ]
Here expr1 and expr2 are any valid test expression, and operator is either && (and) or || (or).
If the && operator is used, both expr1 and expr2 must be true (evaluate to 0) in order for the compound expression to be true.
If the || operator is used, either expr1 or expr2 must be true (evaluate to 0) in order for the compound expression to be true.
A Compound Expression Example Here are two equivalent examples that demonstrate how to create a compound expression:
if [ -z "$DTHOME" ] && [ -d /usr/dt ] ; then DTHOME=/usr/dt ; fi if [ -z "$DTHOME" -a -d /usr/dt ] ; then DTHOME=/usr/dt ; fi
The first version is executed as follows:
[ -z "$DTHOME" ]
[ -d /usr/dt ]
DTHOME=/usr/dt
Execution of the second version is similar:
-z "$DTHOME"
-d /usr/dt
DTHOME=/usr/dt
Some programmers prefer the version that uses the conditional operators because the individual tests are isolated. Other programmers prefer the second form because it invokes the test command only once and might be marginally more efficient on older hardware or for large numbers of tests.
If you are interested in maximum portability to older systems, you should use conditional operators. On modern shells both forms work equally well, thus you can use either one.
In this example you used only two expressions. You are not limited to two. In fact any number of expressions can be combined into one compound expression.
Negating an Expression
The final type of compound expression consists of negating an expression. Negation reverses the result of an expression. True expressions are treated as false expressions and vice versa.
The basic syntax of the negation operator is
test ! expr
or
[ ! expr ]
Here expr is any valid test expression.
A simple example is the following command:
$ if [ ! -d $HOME/bin ] ; then mkdir $HOME/bin ; fi
Here you make the directory $HOME/bin if it does not exist. The execution is as follows:
-d $HOME/bin
A shorter form of the same command is the following:
$ test ! -d $HOME/bin && mkdir $HOME/bin
This command achieves the same result because mkdir executes only if test returns true. test returns true only if the directory $HOME/bin does not exist.
Previous | Table of Contents | Next |