Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash | ||
---|---|---|
Prev | Chapter 3. Tutorial / Reference | Next |
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.
command-1 && command-2 && command-3 && ... command-n |
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 |
command-1 || command-2 || command-3 || ... command-n |
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.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |