Magazine

Using Switching Case in Linux Bash Script

Posted on the 14 April 2021 by Satish Kumar @satish_kumar86

Apart from simplebrancheswithif, it is also possible to process multiple decision-making operations using thecasecommand. In acasestatement, the expression contained in a variable is compared with a number of expressions, and for each expression matched, a command is executed.

It is possible to have multiple branches using theif/elif/elsecommands. But if more than two or threeelifcommands are used, then code becomes very complex. When all the different conditions are depending on a single variable, in such cases, theesacstatement is used. The interpreter checks the value of thecasevariable againstvalue1,value2,value3, and so on, till the match is found. If the value is matched, then all the statements after thatcasevalue are executed till the double semicolon is reached. If nothing is matched, then statements afteresacare executed. Wildcard characters and pipes (vertical bar forORingtwo values) are allowed in thecasestatement.

Acasestatement has the following structure:

case variable in 
  value1) 
    command(s) 
    ;; 
  value2) 
    command(s) 
    ;; 
  *) 
    command(s) 
          ;; 
esac 

For illustrating the switch case scripting example, we will write the case_01.sh script as follows. We will ask the user to enter any number from the range 1-9. We will check the entered number with the case command. If a user enters any other number, then we will display the error by displaying the Invalid key message:

case_01.sh
#!/bin/bash 
 
echo "Please enter any number from 1 to 9" 
read number 
 
case $number in 
  1) echo "ONE" 
    ;; 
  2) echo "TWO" 
    ;; 
  3) echo "Three" 
    ;; 
  4) echo "FOUR" 
      ;; 
  5) echo "FIVE" 
    ;; 
  6) echo "SIX" 
    ;; 
  7) echo "SEVEN" 
    ;; 
  8) echo "EIGHT" 
    ;; 
  9) echo "NINE" 
    ;; 
  *) echo "SOME ANOTHER NUMBER" 
    ;; 
esac

Let’s test the program:

$ chmod +x case_01.sh
$ ./case_01.sh

The following will be the output after executing the preceding commands:

Output:
Please enter any number from 1 to 95FIVE

Sometimes, in the shell script we may need to ask for an email address from the user. In such situations, we need to verify whether the address is correct. We can use thecasecommand to validate the correct email address as follows:

#!/bin/bash 
case $1  in 
*@*.com)   echo "valid email address" 
    ;; 
*)    echo "invalid string" 
    ;; 
esac

Let’s test the program:

$ chmod +x case_02..sh
$ ./case_02.sh   [email protected]

The following will be the output after executing the preceding commands if the email address is correct:

Output:
valid email address
$ ./case_02.sh abc.com

The following will be the output after executing the preceding commands if the email address is not correct:

Output:
invalid string

If, inside the script we need to provide file operations such as copy, move, or delete, then we can use the case command for such scripts. The script case_03.sh for file operations is as follows:

case_03.sh
#!/bin/bash 
echo "Press 1 for copy or 2 for move or 3 for removing the file" 
read num 
case $num in 
1)  echo "We are going to do copy operation" 
echo " Enter Source file name" 
read source 
echo " Enter destination file name" 
read destination 
cp $source $destination 
;; 
2)   echo "We are going to do move operation" 
echo " Enter Source file name" 
read source 
echo "Enter destination file name" 
read destination 
mv $source $destination   ;; 
3)   echo "We are going to remove the file" 
echo " Enter the name of file to remove" 
read source 
rm $source   ;; 
*) echo "invalid key" 
esac

Let’s test the program:

$ chmod +x case_03.sh
$ ./case_03.sh

The following will be the output after executing the preceding commands:

Output:
Press 1 for copy or 2 for move or 3 for removing the file1
We are going to do copy operation 
Enter Source file nameFile1 
Enter destination file nameFile4

In this shell script case_04.sh, we will ask the user to enter the day of the week. Inside the script, we will detect the text entered and print a detailed description of the day such as First Day is Monday and similar on the screen. Note that we are able to perform pattern matching for the uppercase and lowercase in the case statement:

case_04.sh
#!/bin/bash 
echo "Enter Day Of The Week" 
read day 
 
case $day in 
  [mM][oO][nN][dD][aA][yY]) 
        echo "First Day is Monday" 
        ;; 
  [tT][uU][eE][sS][dD][aA][yY]) 
        echo "Second Day Tuesday" 
        ;; 
  [wW][eE][dD][nN][eE][sS][dD][aA][yY]) 
        echo "Third Day Wednesday" 
        ;; 
  [tT][hH][uU][rR][sS][dD][aA][yY]) 
        echo " Fourth Day Thursday" 
        ;; 
  [fF][rR][iI][dD][aA][yY]) 
        echo "Fifth Day Friday" 
        ;; 
  [sS][aA][tT][uU][rR][dD][aA][yY]) 
        echo "Sixth Day Saturday" 
        ;; 
  [sS][uU][nN][dD][aA][yY]) 
        echo "Seventh Day Sunday" 
        ;; 
  *) 
    echo "Invalid Day of Week" 
    ;; 
   esac

Let’s test the program:

$ chmod +x case_04.sh
$ ./case_04.sh

The following will be the output after executing the preceding commands:

Output:
$ ./case_04.shEnter Day Of The WeekMondayFirst Day is Monday
$ ./case_04.shEnter Day Of The WeekThursdayFourth Day Thursday

We write the scriptcase_05.shfor printing days in the current month. We will use thedatecommand in the script for finding the current month:

case_05.sh
#!/bin/bash 
mth=$(date +%m) 
 
case $mth in 
02) 
  echo "February usually has 28 days." 
  echo "If it is a leap year, it has 29 days." 
  ;; 
 
04|06|09|11) 
  echo "The current month has 30 days." 
  ;; 
 
*) 
  echo "The current month has 31 days." 
  ;; 
 esac

Let’s test the program:

$ chmod +x case_05.sh
$ ./case_05.sh

The following will be the output after executing the preceding commands:

Output:
The current month has 30 days.

Back to Featured Articles on Logo Paperblog