Table of Contents#
- Syntax of the
caseCommand - Simple Example of
caseCommand - Using Wildcards in
caseStatements - Multiple Patterns in
case - Common Practices
- Best Practices
- Conclusion
- References
Syntax of the case Command#
The basic syntax of the case command in Linux is as follows:
case expression in
pattern1)
statements1
;;
pattern2)
statements2
;;
pattern3)
statements3
;;
*)
default_statements
;;
esacexpression: This is the variable or value that you want to test.pattern1,pattern2, etc.: These are the possible values or patterns against which theexpressionis compared.statements1,statements2, etc.: These are the commands or actions that will be executed if the corresponding pattern matches theexpression.;;: This is used to terminate each pattern - statement block.*): This is a wildcard pattern that acts as a default case. It will match any value that does not match the previous patterns.esac: This is the end marker for thecasestatement, which is justcasespelled backwards.
Simple Example of case Command#
Let's consider a simple example where we have a variable day and we want to print a message based on its value.
#!/bin/bash
day="Monday"
case $day in
Monday)
echo "It's the start of the workweek."
;;
Friday)
echo "Yay! It's almost the weekend."
;;
Saturday|Sunday)
echo "It's the weekend, time to relax."
;;
*)
echo "It's a regular weekday."
;;
esacIn this example, since the value of day is "Monday", the output will be:
It's the start of the workweek.
Using Wildcards in case Statements#
Wildcards can be used in case patterns to match a set of values. The most commonly used wildcards are * (matches any number of any characters) and ? (matches any single character).
#!/bin/bash
file="document.txt"
case $file in
*.txt)
echo "This is a text file."
;;
*.pdf)
echo "This is a PDF file."
;;
*)
echo "Unknown file type."
;;
esacIn this example, the *.txt pattern matches any file name that ends with .txt, so the output will be:
This is a text file.
Multiple Patterns in case#
You can specify multiple patterns for a single block of statements by separating them with a vertical bar |.
#!/bin/bash
fruit="apple"
case $fruit in
apple|banana|cherry)
echo "This is a common fruit."
;;
kiwi|mango)
echo "This is an exotic fruit."
;;
*)
echo "I don't know this fruit."
;;
esacIn this example, since the value of fruit is "apple", the output will be:
This is a common fruit.
Common Practices#
- Error Handling: Always include a default case (
*) in yourcasestatements. This ensures that if the variable does not match any of the specified patterns, there is a fallback action to handle the situation. - Use Clear Patterns: Choose patterns that are easy to understand and maintain. Avoid using overly complex regular expressions or patterns that are difficult to decipher.
- Indentation: Proper indentation is crucial for readability. Indent the statements inside each pattern block to clearly distinguish them from the pattern itself.
Best Practices#
- Keep It Short: If you find yourself with an extremely long
casestatement, consider refactoring your code. You might be able to break the logic into smaller functions or use an array and a loop to handle the cases more efficiently. - Test Thoroughly: Before deploying your shell script with
casestatements, test all possible cases to ensure that the correct actions are taken for each input.
Conclusion#
The case command in Linux is a powerful and flexible tool for handling multiple conditional checks in shell scripts. It offers a more readable and concise alternative to nested if - else statements, especially when dealing with multiple possible values of a variable. By following the common and best practices outlined in this blog, you can write more robust and maintainable shell scripts.
References#
- The Linux Documentation Project: https://tldp.org/LDP/abs/html/testbranch.html
- Bash Reference Manual: https://www.gnu.org/software/bash/manual/bash.html
This blog provides a comprehensive overview of the case command in Linux. With the examples and practices shared, you should now be well - equipped to use the case command effectively in your shell scripting endeavors.