Chapter 26. List Constructs
The and list and or list constructs provide a means of processing a number of commands consecutively. These can effectively replace complex nested if/then or even case statements.
Chaining together commands
- and list
-
command-1 && command-2 && command-3 && ... command-n
|
Each command executes in turn, provided that the previous command has given a return value of true (zero). At the first false (non-zero) return, the command chain terminates (the first command returning false is the last one to execute).
An interesting use of a two-condition and list from an early version of YongYe's Tetris game script:
equation()
{
[[ ${cdx} ]] && ((y=cy+(ccy-cdy)${2}2))
eval ${1}+=\"${x} ${y} \"
}
|
Example 26-1. Using an and list to test for command-line arguments
#!/bin/bash
if [ ! -z "$1" ] && echo "Argument #1 = $1" && [ ! -z "$2" ] && \
echo "Argument #2 = $2"
then
echo "At least 2 arguments passed to script."
else
echo "Fewer than 2 arguments passed to script."
fi
if [ ! -z "$1" ]
then
echo "Argument #1 = $1"
fi
if [ ! -z "$2" ]
then
echo "Argument #2 = $2"
echo "At least 2 arguments passed to script."
else
echo "Fewer than 2 arguments passed to script."
fi
exit $?
|
Example 26-2. Another command-line arg test using an and list
#!/bin/bash
ARGS=1
E_BADARGS=85
test $# -ne $ARGS && \
echo "Usage: `basename $0` $ARGS argument(s)" && exit $E_BADARGS
echo "Correct number of arguments passed to this script."
exit 0
|
Of course, an and list can also set variables to a default value.
arg1=$@ && [ -z "$arg1" ] && arg1=DEFAULT
|
- or list
-
command-1 || command-2 || command-3 || ... command-n
|
Each command executes in turn for as long as the previous command returns false. At the first true return, the command chain terminates (the first command returning true is the last one to execute). This is obviously the inverse of the "and list".
Example 26-3. Using or lists in combination with an and list
#!/bin/bash
E_BADARGS=85
if [ -z "$1" ]
then
echo "Usage: `basename $0` filename"
exit $E_BADARGS
else
file=$1
fi
[ ! -f "$file" ] && echo "File \"$file\" not found. \
Cowardly refusing to delete a nonexistent file."
[ ! -f "$file" ] || (rm -f $file; echo "File \"$file\" deleted.")
exit $?
|
|
If the first command in an or list returns true, it will execute.
|
[ -x /usr/bin/clear ] && /usr/bin/clear
for i in /etc/rc1.d/S[0-9][0-9]* ; do
[ -x "$i" ] || continue
case "$1" in
*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
continue;;
esac
[ "$i" = "/etc/rc1.d/S00single" ] && continue
$i start
done
|
|
The exit status of an and list or an or list is the exit status of the last command executed.
|
Clever combinations of and and or lists are possible, but the logic may easily become convoluted and require close attention to operator precedence rules, and possibly extensive debugging.
false && true || echo false
( false && true ) || echo false
false && ( true || echo false )
|
See Example A-7 and Example 7-4 for illustrations of using and / or list constructs to test variables.