
The bash case statement is a conditional structure that lets you test and execute multiple blocks using variable expressions in a script. It is similar to the if..else statement and uses multiple conditional branches to compare a given variable with a series of patterns and execute a match.
This article explains how to use the case statement in Bash and execute matching patterns. You will use the case statement syntax to create example scripts and set up conditions to run specific commands.
The case statement uses an expression to search for a pattern and stops when a condition is met. A default case is executed if the expression does not match any pattern. Below is the basic case statement syntax.
A valid statement starts with case, the expression to test, and in that starts the list of patterns to check if the expression meets any of the available conditions. Each pattern is terminated with ) while the included clause terminates with ;;.
The wildmask character * sets the default pattern to match if no condition is met within the statement. When no pattern is met including the default, the return status is 0 or the exit status of the expression. Multiple pattern options are separated using the OR operator | and the keyword esac closes a case statement.
The case statement is compatible with multiple types of expressions such as commands and datasets. In the following example, use case statement to test for the installation of a particular application in a LAMP (Linux, Apache, MySQL, PHP) stack.
Create a new bash script LAMP.sh using your a text editor such as Vim.
Add the following code to the file.
Save and close the file.
The above Bash script displays a list of LAMP stack applications and reads the user input to store a user's selection in a new app variable. The case statement compares the variable data with all defined patterns to display the installation command for the selected application. A joint installation command is listed when the user’s selection does not match any pattern.
Run the script using Bash to test the case statement.
Output:
Select a specific result from the list and enter the value. For example, MySQL.
The case statement executes a pattern that matches the MySQL condition with the following output before exiting.
Run the script again.
Enter an invalid selection such as Nginx.
Verify that the default case statement is executed with the following output.
Nested case statements enable the execution of multiple levels of conditions if an expression matches a specific pattern. A nested case statement contains two or more conditions to filter a pattern with a specific expression to handle a combination of results. Use the following example to create nested case statements to test a LAMP stack selection and install an application based on the user’s input.
Create a new bash script nested-lamp.sh.
Add the following code to the file.
Save and close the file.
In the above example, the user’s original LAMP stack application selection matches a specific pattern that's tested to either install the application or update the server packages without installation. A default pattern is executed in each statement if the user request does not match any pattern.
Run the script.
Output:
Enter a value such as Apache and press Enter to save your selection.
Enter an integer value to make a selection and install the application or update the server packages. For example, enter 2.
Verify that your server updates before the script exits.
Test the script return code and verify that it’s zero which represents a successful execution.
Output:
Run the script again.
Enter a new application value. For example, PHP.
Enter an invalid integer value such as 5 when prompted to install or update the server.
Verify that the default pattern executes when the value does not match any condition similar to the following output.
Test the script return code and verify that it's 1 meaning an error occurred due to an invalid selection.
Output:
You have used the case statement in Bash to enables the execution of specific commands when an expression matches any of the available patterns. The case statement is efficient, uses a default case when a condition does not meet any pattern, and improves your script readability as compared to other conditional statements such as if..else.
0 Comments
Be the first to comment and share your perspective with the community.