The switch case in BASH is more relevant and is widely used among the Linux admins/Devops folks to leverage the power of control flow in shell scripts.
As we have seen the if..elif..else..fi
Control Structure: Bash If then Else. The switch case has a stronger case where it really simplifies out the control flow by running the specific block of bash code based on the user selection or the input parameters.
Let’s take a look at the simple Switch case as follows:
OPTION=$1 case $OPTION in choice1) Choice1 Statements ;; choice2) Choice2 Statements ;; choiceN) ChoiceN Statements ;; *) echo “User Selected Choice not present” exit 1 esac
The OPTION
is generally read from user input and upon this the specific choice case block is invoked.
Explanation:
In the switch command the control flow is forwarded to case keyword and stops here, it checks for the suitable match to pass over the control to relevant OPTION
/CHOICE
statement block. Upon the execution of the relevant CHOICE statements the case is exited once the control flow encounters esac
keyword at the end.
Using the Pattern match
The control flow in bash identifies the case options and proceeds accordingly.
There can be cases where you can match the Here you might have observed that the user input the regular expression and the logical operators using the | for the input case
#! /bin/bash echo -en "Enter your logins\nUsername: " read user_name echo -en "Password: " read user_pass while [ -n $user_name -a -n $user_pass ] do case $user_name in ro*|admin) if [ "$user_pass" = "Root" ]; then echo -e "Authentication succeeded \ n You Own this Machine" break else echo -e "Authentication failure" exit fi ;; jenk*) if [ "$user_pass" = "Jenkins" ]; then echo "Your home directory is /var/lib/jenkins" break else echo -e "Authentication failure" fi break ;; *) echo -e "An unexpected error has occurred." exit ;; esac done
You should kindly note that the regex used for the cases at ro*|admin
and jenk*
We now have demonstrated by entering the username as jenkins
and this will get matched with the jenkins case the control flow successfully enters into relevant block of code, checking the password match or not is not relevant for us as we are only concerned till the case choice selection.
We have named the switch case into a script switch-case.sh and run it, Here are the results.
OUTPUT :
[vamshi@node02 switch-case]$ sh switch-case.sh Enter your logins Username: jenkins Password: Jenkins Your home directory is /var/lib/jenkins
We have entered the correct password and successfully runs the jenkins case block statements
We shall also see the or ro*|admin case, demonstrated as follows.
[vamshi@node02 switch-case]$ sh switch-case.sh Enter your logins Username: root Password: Root Authentication succeeded \ n You Own this Machine
We now test the admin
username and see the results.
[vamshi@node02 switch-case]$ sh switch-case.sh Enter your logins Username: admin Password: Root Authentication succeeded \ n You Own this Machine
Here is a more advanced script used to deploy a python application using the switch case..
Please refer to the Command line arguments section for user input
A complete functional Bash switch case can be seen at https://github.com/rrskris/python-deployment-script/blob/master/deploy-python.sh
Please feel free to share your experiences in comments.