In this subject, we'll discuss the fundamentals of case statements and the way to use them in Bash scripts.
The Bash case statement is that the simplest sort of IF-THEN-ELSE with many ELIF elements. Utilizing the case statement makes our bash script or program more readable and more manageable to take care of. These are ordinarily utilised to interpret the complicated conditions having various different options.
The Bash case statement follows an identical logic because of the Javascript or C switch statement. there's a small difference, as follows:
The Bash case statement demands excellence once and tests that value various times. It suspends checking out a pattern once it's received it and performed the statement combined with it, which is approximately the other inside the case of the C switch statement.
Case Statement Syntax
Let's have look at the syntax of the bash case:
case expression in
pattern_1)
statements
;;
pattern_2)
statements
;;
pattern_3|pattern_4|pattern_5)
statements
;;
pattern-n)
statements
;;
*)
statements
;;
esac
There are many key features of bash case statements:
Individually case statement in bash commences with the 'case' keyword, accompanied by the case expression and 'in' keyword. The case declaration is ended by 'esac' keyword.
We can utilize complicated patterns severed by | operator. The ‘)’ implies the termination of a pattern catalog.
A pattern including the statements is designated as a clause, and it should be stopped by a double semicolon (;;).An asterisk symbol (*) is employed as a final pattern to define the default case. it's used as a default case when used because of the last case.
How it works
First of all, the case statement expands the expression and tries to match each of the included patterns. When it finds a match up, all the connected statements are implemented till the double semicolon (;;). After the primary match, the case terminates with the exit status of the last executed statement.
If there's no similar pattern, the exit status of the case is zero. Oppositely, the return status is that the exit status of the executed statements.
If the default asterisk pattern is employed, it'll be executed just in case of no matched pattern.
Let's attempt to understand this mechanism with the assistance of a couple of examples:
Example1:
!/bin/bash
echo "Do you know Java Programming?"
read -p "Yes/No? :" Answer
case $Answer in
Yes|yes|y|Y)
echo "That's amazing."
echo
;;
No|no|N|n)
echo "It's easy. Let's start learning from Great Learning Academy, We have free Cources on java which will assist while learning."
;;
esac
Here is the Output of the given Command:
Example 2:
In this example, we've defined a combined scenario where there's also a default case when no previous matched case is found.
#!/bin/bash
echo "Which Operating System are you using?"
echo "Windows, Android, Chrome, Linux, Others?"
read -p "Type your OS Name:" OS
case $OS in
Windows|windows)
echo "That's common. You should try something new."
echo
;;
Android|android)
echo "This is my favorite. It has lots of applications."
echo
;;
Chrome|chrome)
echo "Cool!!! It's for pro users. Amazing Choice."
echo
;;
Linux|linux)
echo "You might be serious about security!!"
echo
;;
*)
echo "Sounds interesting. I will try that."
echo
;;
esac
Output Of the above code snippet: