Bash Scripting – Working with the if-then Statement
The most basic type of structured command is the if-then statement. The if-then statement has the following format:
Syntax
if command then commands fi
If you’re using if-then statements in other programming languages, this format may be somewhat confusing. In other programming languages, the object after the if statement is an equation that is evaluated for a TRUE or FALSE value. That’s not how the bash shell if statement works.
The bash shell if statement runs the command defined on the if line. If the exit status of the command is zero (the command completed successfully), the commands listed under the then section are executed. If the exit status of the command is anything else, the then commands aren’t executed, and the bash shell moves on to the next command in the script. The fi statement delineates the if-then statement’s end.
Here’s a simple example to demonstrate this concept:
Example 1
$ cat test1.sh #!/bin/bash # testing the if statement if pwd then echo “It worked” fi $
This script uses the pwd command on the if line. If the command completes successfully, the echo statement should display the text string. When you run this script from the command line, you get the following results:
$ ./test1.sh /home/Christine It worked $
The shell executed the pwd command listed on the if line. Because the exit status was zero, it also executed the echo statement listed in the then section.
Here’s another example:
Example 2
$ cat test2.sh #!/bin/bash # testing a bad command if IamNotaCommand then echo “It worked” fi echo “We are outside the if statement” $ $ ./test2.sh ./test2.sh: line 3: IamNotaCommand: command not found We are outside the if statement $
In this example, we deliberately used a command, IamNotaCommand, that does not work in the if statement line. Because this is a bad command, it produces an exit status that’s non-zero, and the bash shell skips the echo statement in the then section. Also notice that the error message generated from running the command in the if statement still appears in the script’s output. There may be times when you don’t want an error statement to appear.
NOTE
You might see an alternative form of the if-then statement used in some scripts:
Syntax 2
if command; then commands fi
By putting a semicolon at the end of the command to evaluate, you can include the then statement on the same line, which looks closer to how if-then statements are handled in some other programming languages.
You are not limited to just one command in the then section. You can list commands just as in the rest of the shell script. The bash shell treats the commands as a block, executing all of them when the command in the if statement line returns a zero exit status or skipping all of them when the command returns a non-zero exit status:
Example 3
$ cat test3.sh #!/bin/bash # testing multiple commands in the then section # testuser=Christine # if grep $testuser /etc/passwd then echo “This is my first command” echo “This is my second command” echo “I can even put in other commands besides echo:” ls -a /home/$testuser/.b* fi $
The if statement line uses the grep comment to search the /etc/passwd file to see if a specific username is currently used on the system. If there’s a user with that logon name, the script displays some text and then lists the bash files in the user’s HOME directory:
$ ./test3.sh Christine:x:501:501:Christine B:/home/Christine:/bin/bash This is my first command This is my second command I can even put in other commands besides echo: /home/Christine/.bash_history /home/Christine/.bash_profile /home/Christine/.bash_logout /home/Christine/.bashrc $
However, if you set the testuser variable to a user that doesn’t exist on the system, nothing happens:
$ cat test3.sh #!/bin/bash # testing multiple commands in the then section # testuser=NoSuchUser # if grep $testuser /etc/passwd then echo “This is my first command” echo “This is my second command” echo “I can even put in other commands besides echo:” ls -a /home/$testuser/.b* fi $ $ ./test3.sh $
It’s not all that exciting. It would be nice if we could display a little message saying that the username wasn’t found on the system. Well, we can, using another feature of the if-then statement.