| 1 | #!/bin/bash |
| 2 | # and list |
| 3 | |
| 4 | if [ ! -z "$1" ] && echo "Argument #1 = $1" && [ ! -z "$2" ] && \ |
| 5 | # ^^ ^^ ^^ |
| 6 | echo "Argument #2 = $2" |
| 7 | then |
| 8 | echo "At least 2 arguments passed to script." |
| 9 | # All the chained commands return true. |
| 10 | else |
| 11 | echo "Fewer than 2 arguments passed to script." |
| 12 | # At least one of the chained commands returns false. |
| 13 | fi |
| 14 | # Note that "if [ ! -z $1 ]" works, but its alleged equivalent, |
| 15 | # "if [ -n $1 ]" does not. |
| 16 | # However, quoting fixes this. |
| 17 | # if "[ -n "$1" ]" works. |
| 18 | # ^ ^ Careful! |
| 19 | # It is always best to QUOTE the variables being tested. |
| 20 | |
| 21 | |
| 22 | # This accomplishes the same thing, using "pure" if/then statements. |
| 23 | if [ ! -z "$1" ] |
| 24 | then |
| 25 | echo "Argument #1 = $1" |
| 26 | fi |
| 27 | if [ ! -z "$2" ] |
| 28 | then |
| 29 | echo "Argument #2 = $2" |
| 30 | echo "At least 2 arguments passed to script." |
| 31 | else |
| 32 | echo "Fewer than 2 arguments passed to script." |
| 33 | fi |
| 34 | # It's longer and more ponderous than using an "and list". |
| 35 | |
| 36 | |
| 37 | exit $? |