3.19. 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. Note that the exit status of an "and list" or an "or list" is the exit status of the last command executed.

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. At the first false return, the command chain terminates (the first command returning false is the last one to execute).

Example 3-69. Using an "and list" to test for command-line arguments

#!/bin/bash

# "and list"

if [ ! -z $1 ] && echo "Argument #1 = $1" && [ ! -z $2 ] && echo "Argument #2 = $2"
then
  echo "At least 2 arguments to script."
  # All the chained commands return true.
else
  echo "Less than 2 arguments to script."
  # At least one of the chained commands returns false.
fi  
# Note that "if [ ! -z $1 ]" works, but its supposed equivalent,
# "if [ -n $1 ]" does not. This is a bug, not a feature.


# This accomplishes the same thing, coded using "pure" if/then statements.
if [ ! -z $1 ]
then
  echo "Argument #1 = $1"
fi
if [ ! -z $2 ]
then
  echo "Argument #2 = $2"
  echo "At least 2 arguments to script."
else
  echo "Less than 2 arguments to script."
fi
# It's longer and less elegant than using an "and list".


exit 0
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 3-70. Using "or lists" in combination with an "and list"

#!/bin/bash

# "Delete", not-so-cunning file deletion utility.
# Usage: delete filename

if [ -z $1 ]
then
  file=nothing
else
  file=$1
fi  
# Fetch file name (or "nothing") for deletion message.


[ ! -f $1 ] && echo "$1 not found. Can't delete a nonexistent file."
# AND LIST, to give error message if file not present.

[ ! -f $1 ] || ( rm -f $1; echo "$file deleted." )
# OR LIST, to delete file if present.
# ( command1 ; command2 ) is, in effect, an AND LIST variant.

# Note logic inversion above.
# AND LIST executes on true, OR LIST on false.

[ ! -z $1 ] ||  echo "Usage: `basename $0` filename"
# OR LIST, to give error message if no command line arg (file name).

exit 0

Clever combinations of "and" and "or" lists are possible, but the logic may easily become convoluted and require extensive debugging.