Updated April 10, 2023
Introduction to Shell Script Case
The case statement is one of such utility that enables the user to build some functionality without having to get into a lot of hassle handling the branching conditions. Imagine a condition where you would have to apply a lot of if conditions with different level of branching at different levels, for example in some code we might have to check for 20-25 conditions, it is very difficult in developing the loop and even harder is the effort needed to maintain or update them. At that point, case statements come to the rescue. If someone wants to simply visualize what a case statement does, it can be answered as: the 20-25 conditions which need to be checked are checked at different stages without having to branch them under any other if else loop and eventually brings up cleanliness in the way the code is written, developed and maintained.
Explanation of Shell Script Case
The case … esac statement as it is called, starts with a case, and once when the user wants to end the loop, enters esac and in between these keywords are the lines which include conditions which one would need to check for in terms of pattern. Several statements in-between case … esac would be executed to find a match for the pattern which will be specified at the start of the condition. One more thing to keep in mind is that, in the case of many conditions been checked for, if none of the condition matches, there should always be a default condition to fall out for and be used in any of the default conditions. What the interpreter does is that the expression is checked against each value until a match is found. In cases of no match of the expression, the default condition statements will be executed. The syntax is as follows:
case <value> in 1st pattern) Statement(s) to be executed if 1st pattern matches ;; 2nd pattern) Statement(s) to be executed if 2nd pattern matches ;; 3rd pattern) Statement(s) to be executed if 3rd pattern matches ;; *) In case of no pattern matching these statement(s) will be executed. ;; esac
One clear thing we can notice is that the default statements are mentioned under *). This pattern is like a keyword for any fallback options in case of case statements. We would understand this statement through an example in our next section and also look at some output of different cases to help give you a real flavor of the theory topic.
Now one major thing it would strike your mind at this point in time is how does it play in day to day scripting and if there are any major scenarios where these case statements are used. In Linux world, one would have frequently come across the init script, which is nothing but a script that helps in configuring the daemons of the Linux system. As a part of the boot process, the init scripts would run to start the required processes. There are a lot of other technicalities involved in init scripts that are outside the scope of this article and we would reserve it for some other article. So, as a part of searching what process one would need to execute as a part of init script to accomplish the functionalities, the case statement would be the most powerful option. These functionalities include start, stop, restart, etc.
The last thing one should keep in mind for these case statements is that post successful condition testing the exit status of 0 will be sent to the parent mentioning that there are no failures and the parent script can proceed with the next necessary steps. And in case of failure, the corresponding exit code would be returned, and the developer can take appropriate steps to tackle the same.
Examples of Shell Script Case
Now it is time for us to get more comfortable with case commands in shell scripting. As we promised you that we would go through some cases of showing you important usage of the case in shell scripting so that one can appreciate the simplicity case statements beings to the table.
In the following example we would try to compare a nested if a loop with a case statement to show the easiness of using case statement and with the same example, we would try to explain the chronology of the execution of the case statement.
Code for Nested if:
echo "*********Nested if statement************" if [ "$1" == "UK" ] then echo "Country is UK" else if [ "$1" == "US" ] then echo "Country is US" else if [ "$1" == "India" ] then echo "Country is India" fi fi fi
In the above code notice the complexity of branching in comparing just 3 values. Just think about the hassle one would be taking when one has to check 20-25 variables.
Code for case statement:
echo "*********Case statement************" case $1 in "UK") echo "Country is UK" ;; "US") echo "Country is US" ;; "India") echo "Country is India" ;; *) echo "The country entered is not in the list" ;; esac
In the above code, we can very well see how organized the case statements are for case commands and how scalable is this methodology over nested if, in case one needs to compare 20-25 variables with an advantage of no branching required!
In the above syntax, if one needs to understand and maintain the code for any future changes, it becomes simple even if the code is not written by the individual. For example, in this code, if one enters the US or UK or India it will go to the corresponding block of commands in that matching case and execute those commands. Now if somebody has to enter another country in the list, only another case would be added with no confusion of branching!
In conclusion, the case command is a simplified version of using a nested if then else condition without having the user to think and worry about branching not only during development but also during the addition of any other condition check! Also, as a matter of fact, one case also uses OR operator in checking patterns in the switch case making it even more flexible and versatile. Thus, in a nutshell, the easiness case command brings to the table in well appreciated and accepted by the coding fraternity!
We hope that this EDUCBA information on “Shell Script Case” was beneficial to you. You can view EDUCBA’s recommended articles for more information.