At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.
The most compact syntax of the if command is:
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi
The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor "N" is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.
The table below contains an overview of the so-called "primaries" that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.
Table 7-1. Primary expressions
Expressions may be combined using the following operators, listed in decreasing order of precedence:
Table 7-2. Combining expressions
The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening angular bracket should be closed after the conditions have been listed.
The CONSEQUENT-COMMANDS list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi. It is important to remember that the then and fi are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semi-colon.
In a script, the different parts of the if statement are usually well-separated. Below, a couple of simple examples.
The first example checks for the existence of a file:
anny ~> cat msgcheck.sh #!/bin/bash echo "This scripts checks the existence of the messages file." echo "Checking..." if [ -f /var/log/messages ] then echo "/var/log/messages exists." fi echo echo "...done." anny ~> ./msgcheck.sh This scripts checks the existence of the messages file. Checking... /var/log/messages exists. ...done.
To add in your Bash configuration files:
# These lines will print a message if the noclobber option is set: if [ -o noclobber ] then echo "Your files are protected against accidental overwriting using redirection." fi
The ? variable holds the exit status of the previously executed command (the most recently completed foreground process).
The following example shows a simple test:
anny ~> if [ $? -eq 0 ] More input> then echo 'That was a good job!' More input> fi That was a good job! anny ~>
The following example demonstrates that TEST-COMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:
anny ~> if ! grep $USER /etc/passwd More input> then echo "your user account is not managed locally"; fi your user account is not managed locally anny > echo $? 0 anny >
The same result can be obtained as follows:
anny > grep $USER /etc/passwd anny > if [ $? -ne 0 ] ; then echo "not a local account" ; fi not a local account anny >
The examples below use numerical comparisons:
anny > num=`wc -l work.txt` anny > echo $num 201 anny > if [ "num" > 150 ] More input> then echo ; echo "you've worked hard enough for today." More input> echo ; fi you've worked hard enough for today. anny >
This script is executed by cron every Sunday. If the week number is even, it reminds you to put out the garbage cans:
#!/bin/bash # Calculate the week number using the date command: WEEKOFFSET=$[ $(date +"%V") % 2 ] # Test if we have a remainder. If not, this is an even week so send a message. # Else, do nothing. if [ $WEEKOFFSET -eq "0" ]; then echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" your@your_domain.org
An example of comparing strings for testing the user ID:
if [ "$(whoami)" != 'root' ]; then echo "You have no permission to run $0 as non-root user." exit 1; fi
Regular expressions may also be used:
anny > gender="female" anny > if [[ "$gender" == "f*" ]] More input> then echo "Pleasure to meet you, Madame."; fi Pleasure to meet you, Madame. anny >
See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.
Meet new people