7.2.1. if/then/else constructs

7.2.1.1. Dummy example This is the construct to use to take one course of action if the if commands test true, and another if it tests false. An example: freddy scripts> gender = "male" freddy scripts> if [[ "$gender" == "f*" ]] More input> then echo "Pleasure to meet you, Madame." More input> else echo "How come the lady hasn't got a drink yet?" More input> fi How come the lady hasn't got a drink yet? freddy scripts> [] vs. [[]] Contrary to [ , [[ prevents word splitting of variable values. So, if VAR="var with spaces" , you do not need to double quote $VAR in a test - eventhough using quotes remains a good habit. Also, [[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames. Using [[ , == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[ "value" == val* ]] . Like the CONSEQUENT-COMMANDS list following the then statement, the ALTERNATE-CONSEQUENT-COMMANDS list following the else statement can hold any UNIX-style command that returns an exit status. Another example, extending the one from Section 7.1.2.1: anny ~> su - Password: [root@elegance root]# if ! grep ^$USER /etc/passwd 1> /dev/null > then echo "your user account is not managed locally" > else echo "your account is managed from the local /etc/passwd file" > fi your account is managed from the local /etc/passwd file [root@elegance root]# We switch to the root account to demonstrate the effect of the else statement - your root is usually a local account while your own user account might be managed by a central system, such as an LDAP server.

7.2.1.2. Checking command line arguments Instead of setting a variable and then executing a script, it is frequently more elegant to put the values for the variables on the command line. We use the positional parameters $1 , $2 , ..., $N for this purpose. $# refers to the number of command line arguments. $0 refers to the name of the script. The following is a simple example: Figure 7-1. Testing of a command line argument with if Here's another example, using two arguments: anny ~> cat weight.sh #!/bin/bash # This script prints a message about your weight if you give it your # weight in kilos and height in centimeters. weight="$1" height="$2" idealweight=$[$height - 110] if [ $weight -le $idealweight ] ; then echo "You should eat a bit more fat." else echo "You should eat a bit more fruit." fi anny ~> bash -x weight.sh 55 169 + weight=55 + height=169 + idealweight=59 + '[' 55 -le 59 ']' + echo 'You should eat a bit more fat.' You should eat a bit more fat.

7.2.1.3. Testing the number of arguments The following example shows how to change the previous script so that it prints a message if more or less than 2 arguments are given: anny ~> cat weight.sh #!/bin/bash # This script prints a message about your weight if you give it your # weight in kilos and height in centimeters. if [ ! $# == 2 ]; then echo "Usage: $0 weight_in_kilos length_in_centimeters" exit fi weight="$1" height="$2" idealweight=$[$height - 110] if [ $weight -le $idealweight ] ; then echo "You should eat a bit more fat." else echo "You should eat a bit more fruit." fi anny ~> weight.sh 70 150 You should eat a bit more fruit. anny ~> weight.sh 70 150 33 Usage: ./weight.sh weight_in_kilos length_in_centimeters The first argument is referred to as $1 , the second as $2 and so on. The total number of arguments is stored in $# . Check out Section 7.2.5 for a more elegant way to print usage messages.