Case Statements

Bash Scripting Case Statement Examples

Case Statements


Case statements are generally used to simplify conditional processing. They are used when you have multiple different choices or selections. Case statements are generally easier to read than multiple nested "if" statements.

The basic syntax of the case command is as follows:




#
# Case Statement
#

case $test_variable in
     match1)
        commands
        ....
        ....
        ;;
     match2)
        commands
        ....
        ....
        ;;
     match3)
        commands
        ....
        ....
        ;;
     *)
esac

Multiple Case Statement Syntax



#
# Case Statement
#

case $test_variable in
     match1|match2)
        commands
        ....
        ....
        ;;
     match3|match4)
        commands
        ....
        ....
        ;;
     match5|match6)
        commands
        ....
        ....
        ;;
     *)
esac

In the above examples our variable $test_variable is compared against our match patterns until a match is found. Each case statements begins with "case" and ends with "esac". After each command section you must have two semi-colons ";;". The entry "*)" is a default case that matches anything. If you use this option, it must be located at the bottom of your case statements.



Case Statement Examples


In the example below we first test for a null entry with the test [ -z $1 ]. If you do not enter a value, then the script will exit. If a value is passed, then it is checked against patterns within our case statement. If the entered value does not match any pattern, then the default match "*)"is used to issue a "not recognised" message.



#!/bin/bash
#
# Case Statement
#
if [ -z $1 ]; then
   echo "You must specify a model name. e.g. Fiesta"
   exit
fi

case $1 in
     fiesta|Fiesta|FIESTA)
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     mondeo|Mondeo|MONDEO)
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     astra|Astra|ASTRA)
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     insignia|Insignia|INSIGNIA)
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     *)
        echo "Sorry, car $1 is not recognised"
esac

Output from above script



john@john-desktop:~/scripts$ ./case2.sh Fiesta
You entered Fiesta
The model Fiesta is manufactured by Ford
john@john-desktop:~/scripts$ ./case2.sh fiesta
You entered fiesta
The model fiesta is manufactured by Ford
john@john-desktop:~/scripts$ ./case2.sh FIESTA
You entered FIESTA
The model FIESTA is manufactured by Ford
john@john-desktop:~/scripts$ ./case2.sh Astra
You entered Astra
The model Astra is manufactured by Vauxhall
john@john-desktop:~/scripts$ ./case2.sh Lotus
Sorry, car Lotus is not recognised
john@john-desktop:~/scripts$ ./case2.sh 
You must specify a model name. e.g. Fiesta

Mixed Case Pattern matching


The only problem with matching patterns is that they have to match. In the above examples we used various combinations for our patters,"fiesta, Fiesta and FIESTA". This would be fine as long as the user did not't enter "fIesta or fiestA". To get round this problem there are several solutions available that can be used. One method would be to use the translate command "tr":

Our script now contains a line that translates our entry into lowercase: newtest=$( echo "$1" | tr -s '[:upper:]' '[:lower:]' )

We then modify our case test to use the new variable $newtest. Our patterns need to be modified so that they only reference a lowercase name, "fiesta, mondeo, astra or insignia.



#!/bin/bash
#
# Case Statement
#
if [ -z $1 ]; then
   echo "You must specify a model name. e.g. Fiesta"
   exit
fi

# Translate any uppercase characters into lowercase to simplify our test

newtest=$( echo "$1" | tr -s  '[:upper:]'  '[:lower:]' )


case $newtest in
     fiesta)
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     mondeo)
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     astra)
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     insignia)
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     *)
        echo "Sorry, car $1 is not recognised"
esac


An alternative way would be to modify the pattern match as follows (This could be time consuming to code if there were many entries)



#!/bin/bash
#
# Case Statement
#
if [ -z $1 ]; then
   echo "You must specify a model name. e.g. Fiesta"
   exit
fi

case $1 in
     [fF][Ii][Ee][Ss][Tt][Aa] )
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     [Mm][Oo][Nn][Dd][Ee][Oo] )
        echo "You entered $1"
        echo "The model $1 is manufactured by Ford"
        ;;
     [Aa][Ss][Tt][Rr][Aa] )
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     [Ii][Nn][Ss][Ii][Gg][Nn][Ii][Aa] )
        echo "You entered $1"
        echo "The model $1 is manufactured by Vauxhall"
        ;;
     *)
        echo "Sorry, car $1 is not recognised"
esac