Bash 'case' statement - We have been learning about loops in shell scripting and in the recent articles, we have covered two of its kinds - for loop and while loop. Before we move further and discuss about another type of a loop -
select
loop, it is of utmost importance to know about a control flow statement - case
statement. Before we proceed, I recommend you to read our article on another kind of control flow statement - if-elif-else statement in bash scripting.'case' Statement
The
case
statement executes any one block of commands, based on the outcome of a pattern match. We have a variable that stores a value to be matched and a number of patterns in the order they are arranged, which may or may not be regular expressions, against which the value is matched. If a match is found, block of commands corresponding to the pattern is executed. If the value doesn't match any of the patterns, the default block is executed.Syntax:
case $varName in pattern1) ... Block of Commands #1 ... ;; pattern2) ... Block of Commands #2 ... ;; ... ... patternN) ... Block of Commands #N ... ;; *) ... Default Block of Commands ... esac
OR
case $varName in pattern1|pattern2|pattern3) ... Block of Commands #1 ... ;; pattern4|pattern5|pattern6) ... Block of Commands #2 ... ;; pattern7|pattern8|patternN) ... Block of Commands #N ... ;; *) ... Default Block of Commands ... esac
Example:
#!/bin/bash echo "Which is your Favorite Operating System..?" read os case $os in "Linux") echo "Woww!! I am also a Linux Fan!!" ;; "Mac") echo "You must be very Rich!" ;; "Windows") echo "You Should Try Linux Once.. You would love it!" ;; *) echo "I've never used that one!" esac
In above example, we ask the user about his favorite operating system, using
echo
command. We read the user input and store it in $os
variable, with the use of read os
. With this variable used in case
statement as a switch, we compare it against different operating systems, such as Linux
, Windows
and Mac
. The content stored in $os
is matched against these patterns and corresponding block of code is executed. If match is not found, the default block (the one beginning with *)
) is executed.Output:
Which is your Favorite Operating System..?
Linux
Woww!! I am also a Linux Fan!!
Which is your Favorite Operating System..?
Mac
You must be very Rich!
Which is your Favorite Operating System..?
Windows
You Should Try Linux Once.. You would love it!
Which is your Favorite Operating System..?
Unix
I've never used that one!
Great! The script works just fine. But there is a minor bug in that. What if 'Linux' is indeed my favorite and I enter
linux
when prompted for?Which is your Favorite Operating System..?
linux
I've never used that one!
This is not what we wanted. The pattern matching being case sensitive and the patterns may or may not be regular expressions, we can change the pattern for 'Linux' to
[lL]inux
, which will match both upper case and lower case 'L', but it won't match LINUX
. Please Google about 'Regular expressions' for more details.Example:
#!/bin/bash echo "Which is your Favorite Operating System..?" read os case $os in [lL]inux) echo "Woww!! I am also a Linux Fan!!" ;; [mM]ac) echo "You must be very Rich!" ;; [wW]indows) echo "You Should Try Linux Once.. You would love it!" ;; *) echo "I've never used that one!" esac
Output:
Which is your Favorite Operating System..?
linux
Woww!! I am also a Linux Fan!!
Which is your Favorite Operating System..?
Linux
Woww!! I am also a Linux Fan!!
Which is your Favorite Operating System..?
LINUX
I've never used that one!
Example - Using another syntax :
#!/bin/bash echo "Which is your Favorite Operating System..?" read os case $os in "Linux" | "Ubuntu" | "Linux Mint" | "CentOS") echo "Woww!! I am also a Linux Fan!!" ;; "Mac") echo "You must be very Rich!" ;; "Windows") echo "You Should Try Linux Once.. You would love it!" ;; *) echo "I've never used that one!" esac
Output :
Which is your Favorite Operating System..?
CentOS
Woww!! I am also a Linux Fan!!
Which is your Favorite Operating System..?
Ubuntu
Woww!! I am also a Linux Fan!!
Which is your Favorite Operating System..?
Windows
You Should Try Linux Once.. You would love it!
Which is your Favorite Operating System..?
Fedora
I've never used that one!
With this, we close our discussion on
case
statement. In this article, we learned how a basic case
statement is written and used with regular expression matching. In the next article, we resume our discussion on loops in bash scripting and learn about select
loop. Please share your feedback in the comment section below and stay tuned. Thank you!
0 comments:
Post a comment