How to Use the Case statement in Bash?
The Bash case statement is an expressive conditional statement that allows you to match variables and execute corresponding commands against patterns you define in advance. It is similar to the if.else statement but is a more efficient way to handle lots of conditions by branching on variable matches.
This article provides a comprehensive guide on how to employ the case statement in Bash. You will learn the syntax and structure and how to write scripts that execute specific commands based on user input or variable values.
Understanding Case Statement Syntax
Here’s the basic structure of a case statement in Bash:
case EXPRESSION in PATTERN_1) Commands to run if matched ;; *) Default commands if no match ;; esac
The block begins with case followed by the variable (or expression) you want to evaluate.
in marks the start of the different pattern options.
Each PATTERN) ends with a set of commands, which must be terminated with ;;.
* is the wildcard used for the default case when none of the patterns match.
The block ends with esac (which is just case spelled backwards).
You can use the OR operator (|) to handle multiple values for one pattern.
Example: Simple Case Statement
Let’s create a basic script that displays installation commands for applications in a LAMP stack.
Step 1: Create the script file
$ vim LAMP.sh
Step 2: Add the following code:
#!/bin/bash
echo “Below are the Applications in a LAMP Stack, please make a selection:
1. Apache 2. MySQL 3. PHP 4. Install All"
echo “Enter the application to view installation command (Apache, MySQL, PHP, or All):”
read app
case $app in
Apache)
echo “To install Apache, run the following command:”
echo “sudo apt-get install apache2”
;;
MySQL)
echo “To install a MySQL database server, run the following command:”
echo “sudo apt-get install mysql-server”
;;
PHP)
echo “To install PHP, run the following command:”
echo “sudo apt-get install php libapache2-mod-php php-mysql”
;;
All)
echo “Run the following command to install the entire LAMP stack:”
echo “sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mysql”
;;
*)
echo “Ooops! Invalid selection. Install the entire LAMP stack with the following command:”
echo “sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mysql”
;;
esac
Step 3: Run the script
$ bash LAMP.sh
Output:
mathematica
Below are the Applications in a LAMP Stack, please make a selection:
1. Apache
2. MySQL
3. PHP
4. Install All
Enter the application to view installation command (Apache, MySQL, PHP, or All):
MySQL
sudo apt-get install mysql-server
Try running it again and enter an invalid selection like Nginx.
Output:
Ooops! Invalid selection. Install the entire LAMP stack with the following command:
sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mysql
Example: Nested Case Statements
Now let’s take it a step further. You can nest case statements to build more interactive scripts that respond to multiple inputs. Let’s build one that asks follow-up questions based on your original selection.
Step 1: Create a new file
$ vim nested-lamp.sh
Step 2: Add this code:
#!/bin/bash
echo “Below are the Applications in a LAMP Stack, please make a selection:
1. Apache
2. MySQL
3. PHP
4. Install All”
echo “Enter the application to view installation command (Apache, MySQL, PHP, or All):”
read app
case $app in
Apache)
echo “sudo apt install apache2 ”
echo “What would you like to do:
1. Install Apache
2. Update the Server Packages”
read selection
case $selection in
1)
echo “Installing Apache…..”
sudo apt install apache2
;;
2)
echo “Updating the server packages……”
sudo apt update
;;
*)
echo -e “Ooops! You didn’t make a valid selection. Install the entire LAMP stack with the following command: ”
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql”
exit 1
;;
esac
;;
MySQL)
echo “sudo apt install mysql-server ”
echo “What would you like to do:
1. Install MySQL
2. Update the Server Packages ”
read selection
case $selection in
1)
echo “Installing MySQL….. ”
sudo apt install mysql-server
;;
2)
echo “Updating the server packages…… ”
sudo apt update
;;
*)
echo -e “Ooops! You didn’t make a valid selection. Install the entire LAMP stack with the following command: ”
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql”
exit 1
;;
esac
;;
PHP)
echo “sudo apt install php libapache2-mod-php php-mysql ”
echo “What would you like to do:
1. Install PHP and modules
2. Update the Server Packages”
read selection
case $selection in
1)
echo “Installing PHP and modules…..”
sudo apt install php libapache2-mod-php php-mysql
;;
2)
echo “Updating the server packages……”
sudo apt update
;;
*)
echo -e “Ooops! You didn’t make a valid selection. Install the entire LAMP stack with the following command: ”
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql”
exit 1
;;
esac
;;
All)
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql ”
echo “What would you like to do:
1. Install the entire LAMP Stack
2. Update the Server Packages ”
read selection
case $selection in
1)
echo “Installing All….. ”
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
;;
2)
echo “Updating the server packages……”
sudo apt update
;;
*)
echo -e “Ooops! You didn’t make a valid selection. Install the entire LAMP stack with the following command: ”
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql”
exit 1
;;
esac
;;
*)
echo “Ooops! Invalid selection. Install the entire LAMP stack with the following command:”
echo “sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql”
;;
esac
Step 3: Run the script
$ bash nested-lamp.sh
Example output (valid selection):
mathematica
Below are the Applications in a LAMP Stack, please make a selection:
1. Apache
2. MySQL
3. PHP
4. Install All
Enter the application to view installation command (Apache, MySQL, PHP, or All):
Apache
sudo apt install apache2
What would you like to do:
1. Install Apache
2. Update the Server Packages
2
Updating the server packages……
Fetched 55.4 kB in 2s (28.1 kB/s)
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
15 packages can be upgraded. Run ‘apt list –upgradable’ to see them.
Check if the script exited successfully:
$ echo $? 0
Example output (invalid selection):
Enter the application to view installation command (Apache, MySQL, PHP, or All):
PHP
What would you like to do:
1. Install PHP and modules
2. Update the Server Packages
5
Ooops! You didn’t make a valid selection. Install the entire LAMP stack with the following command:
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
Check the exit code:
$ echo $? 1
Conclusion
Using the Bash case statement, you’re able to write more elegant code that responds intelligently to user input. It’s especially valuable for handling large numbers of potential values and gets what would otherwise be ugly if.else code to appear neat. Whether writing simple menus or more sophisticated nested logic, case makes your Bash scripts readable and user-friendly.
Having observed how to employ use cases, try it out in your own scripts by having it accept different inputs from the user and automate common system administration tasks.