Bash Script Decision Making

Decision making statements come in two forms in Bash Script, ‘if’ statements and ‘case’ statements. Both allow a decision to be made between two or more options.

‘if’ Statements

In its most basic form an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.

if [ expression ]
then
    # Statement(s) will execute if the expression is true.
fi

It should be noted, that the spacing either side of the expression is required in order for it to run successfully. The following example checks whether the values of two variables are the same and displays a message if they are.

NUM1=10
NUM2=10

if [ "$NUM1" -eq "$NUM2" ]
then
    echo "NUM1 is equal to NUM2"
fi

This can be extended to execute a statement or statements if the expression is false as follows.

if [ expression ]
then
    # Statement(s) will execute if the expression is true.
else
    # Statement(s) will execute if the expression is false.
fi

This example adds an ‘else’ statement to the one above to output a message if ‘NUM1' and ‘NUM2’ are not equal.

NUM1=10
NUM2=10

if [ "$NUM1" -eq "$NUM2" ]
then
    echo "NUM1 is equal to NUM2"
else
    echo "NUM1 is not equal to NUM2"
fi

The ‘if’ statement can be further extended with the use of ‘elif’. Any number of ‘elif’ statements can be used to extend the decision making process.

if [ expression ]
then
    # Statement(s) will execute if the expression is true.
elif [ expression ]
then
    # Statement(s) will execute if the first expression is false
    # and the second expression is true.
else
    # Statement(s) will execute if both the expressions are false.
fi

Again, the above example, that compares the variables ‘NUM1’ and ‘NUM2’ can be extended to check if they are equal, then check if ‘NUM1’ is greater than ‘NUM2’ and if neither of the conditions are true, output a third message.

NUM1=10
NUM2=10

if [ "$NUM1" -eq "$NUM2" ]
then
    echo "NUM1 is equal to NUM2"
elif [ "$NUM1" -gt "$NUM2" ]
then
    echo "NUM1 is greater than NUM2"
else
    echo "NUM1 is less than NUM2"
fi

‘if’ statements can also be nested one inside another.

NUM1=10
NUM2=10
NUM3=20

if [ "$NUM1" -eq "$NUM2" ]
then
    if [ "$NUM1" -lt "$NUM3" ]
    then
        echo "NUM1 is equal to num2 and less than NUM3"
    fi
else
    echo "NUM1 is not equal to NUM2 or less than NUM3"
fi

Below are some examples of ‘if’ statements using the logical operators discussed in the previous section on Operators.

BOOL1=true
BOOL2=true
BOOL3=false

if [ "$BOOL1" = true -a "$BOOL2" = true ]
then
    # Display a message if BOOL1 and BOOL2 are true.
    echo "Condition 1 is true"
fi
if [ "$BOOL3" = true -o "$BOOL2" = true ]
then
    # Display a message if BOOL3 or BOOL2 are true.
    echo "Condition 2 is true"
fi
if [ "$BOOL3" = true -a "$BOOL2" = true ]
then
    # Display a message if BOOL3 and BOOL2 are true.
    echo "Condition 3 is true"
else
    # Display a message if BOOL3 and/or BOOL2 is not true.
    echo "Condition 3 is not true"
fi
if [ ! "$BOOL3" = true -a "$BOOL2" = true ]
then
    # Display a message if BOOL3 and/or BOOL2 are not true.
    echo "Condition 4 is true"
fi

The results of running the above will be as follows.

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

The following example shows how a file operator, discussed in the section on Operators, can be used within an 'if' statement, to check if a file called 'file.txt' exists or not.

FILE="/mnt/c/demo/file.txt"

if [ -e "$FILE" ]
then
    echo "The file exists."
else
    echo "The file does not exist."
fi

‘case’ Statements

Where there are more than two options in a decision, a ‘case’ statement can be more efficient than using multiple ‘elif’ statements.

case expression in
    expression-result-option1) 
        # statement(s) to execute.
        ;;
    expression-result-option2) 
        # statement(s) to execute.
        ;;
    expression-result-option3) 
        # statement(s) to execute.
        ;;
    *) 
        # statement(s) to execute.
        ;;
esac

A simple example of a ‘case’ statement would be to display a message on screen depending on the value of a variable.

NUM1=3

case "$NUM1" in
    1)
        echo "a equals 1"
        ;;
    2)
        echo "a equals 2"
        ;;
    3)
        echo "a equals 3"
        ;;
    *)
        echo "a is not equal to 1, 2 or 3"
        ;;
esac

A ‘case’ statement can have any number of options. When the two semi colons are reached, the flow of control jumps to the statement following the end of the ‘case’. The final ‘*’ statement is a catch all if none of the other options evaluate to true and is optional.