Making Decisions In Bash Scripts

20th October, 2016 by

The power of scripting really shows itself in its ability to make decisions and choose whether or not to execute a section of code. This is often referred to as flow control. In this article we’ll be looking at the concept of “if… then… else” and how it can be utilized to control which sections of code are executed and how.

If Statements

Here’s an example script using an if statement:

#!/bin/bash

if [ "$1" == "blue" ]; then

    printf "You must be on Earth.n"

else

    printf "What alien planet are you on?n"

fi

Save the script as “skycolor.sh” and then run it providing an argument for the color of the sky. The script will take this value and if the provided color is “blue” then the script will print the line “You must be on Earth” if any other word is provided then the “else” section of the script will be executed instead which will print the line “What alien planet are you on?”

The “else” section of the statement is optional. The minimal usage of an if statement is the word if, followed by a comparison evaluation contained within square brackets. A semicolon and the word then indicating that the following commands should be executed if the evaluation is true. Finally the “fi” indicates the end of the if statement.

Elif Statement

Sometimes you may have more than two blocks of code whose execution is mutually exclusive, for this you can use “elif” short for else if. This acts much the same way as the initial if statement will sit between the initial then commands and before the else. For example:

#!/bin/bash

if [ "$1" == "blue" ]; then

    printf "You must be on Earth.n"

elif [ "$1"== "black" ]; then

    printf "Sounds like it must be night time.n"

else

    printf "What alien planet are you on?n"

fi

You can have as many elif clauses in your if statement as you wish, meaning that you can have separate blocks of code for a multitude of conditions.

Nested Statements

Command lines within an if statement are the same as those outside, the only difference is whether they are executed or not. This is decided by the if statement itself. This also means that another if statement can exist within the commands of an if statement. This is referred to as nesting, and you can have as many nested if statements as you can cope with. Something to note is that the more nested statements you have the harder the code can become to follow and debug for a human.

#!/bin/bash

if [ "$1" == "blue" ]; then

    printf "You must be on Earth.n"

else

    if [ "$1"== "black" ]; then

        printf "Sounds like it must be night time.n"

    else

        printf "What alien planet are you on?n"

    fi

fi

As you can see the above statement achieves the same task as the previous one but uses a nested if statement instead of an elif. Which highlights that a number of times you’ll have alternate solutions you can use to the same problem. Most times there’s no real right or wrong way to do things, just whichever you find easier to implement and debug later.

In these examples we’ve only used an if statement to compare the value of a string in a variable for a match. While this is useful, an if statement can be used for a number of other comparison operations such as comparing the values of numbers, checking for the existence of files and folders and so on. These comparison operations are something we’ll be looking at further in the next post of our Bash script series.

(Visited 9 times, 1 visits today)