Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

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 command’s 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.

Table 10.4 Operators for Creating Compound Expressions

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:

1.  First the test
[ -z "$DTHOME" ]

is performed.
2.  If this test returns 0, the second test
[ -d /usr/dt ]

is performed. Otherwise the if statement finishes.
3.  If the second test returns 0, the variable assignment
DTHOME=/usr/dt

is performed. Otherwise the if statement finishes.

Execution of the second version is similar:

1.  First the expression
-z "$DTHOME"

is evaluated.
2.  If this expression evaluates to 0, the expression
-d /usr/dt

is evaluated. Otherwise the if statement finishes.
3.  If the second expression evaluates to 0, the variable assignment
DTHOME=/usr/dt

is performed. Otherwise the if statement finishes.

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:

1.  First the test
-d $HOME/bin

is performed.
2.  The result of the test is negated because of the ! operator. If the directory $HOME/bin exists, the return value of the compound expression is false (1): otherwise, the return value is true (0).
3.  If the result of the previous step is true, the directory $HOME/bin is created: otherwise, the if statement finishes.

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