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
OPTION is generally read from user input and upon this the specific choice case block is invoked.
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
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
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.
[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.
What is switch in bash?
How do you write a switch case in shell?
In shell scripting switch case is represented using keywords case and esac which will do multilevel branching and checking in a better way than multiple if-else conditions. Switch case will need an expression which it needs to evaluate and need to perform multiple operations based on the outcome of the expression.
How does case work in bash?
5 Bash Case Statement Examples
- Case statement first expands the expression and tries to match it against each pattern.
- When a match is found all of the associated statements until the double semicolon (;;) are executed.
- After the first match, case terminates with the exit status of the last command that was executed.
What is the use of switch case in Unix shell scripting?
Switch case in shell scripts is an efficient alternative to the if-elif-else statement that we learned previously. The concept of the switch case statements is that we provide different cases (conditions) to the statement that, when fulfilled, will execute specific blocks of commands.
Which command is used for switch case in Linux script?
esac statement is to give an expression to evaluate and to execute several different statements based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.
What is a switch or option in command line?
A command line switch (also known as an option, a parameter, or a flag) acts as a modifier to the command you are issuing in the Command Prompt window, in a batch file, or in other scripts. Usually, a switch is a single letter preceded by a forward slash.
What is ESAC bash?
The esac keyword is indeed a required delimiter to end a case statement in bash and most shells used on Unix/Linux excluding the csh family. The original Bourne shell was created by Steve Bourne who previously worked on ALGOL68. This language invented this reversed word technique to delimit blocks.
What is Getopts in shell script?
getopts is a built-in Unix shell command for parsing command-line arguments. It is designed to process command line arguments that follow the POSIX Utility Syntax Guidelines, based on the C interface of getopt. The predecessor to getopts was the external program getopt by Unix System Laboratories.