The case
statement in shell scripting provides a clean and efficient way to handle multiple conditions without nested if-else
statements. This guide will explore various uses of case statements with practical examples.
Basic Syntax
The basic syntax of a case statement is:
case EXPRESSION in
PATTERN1)
STATEMENTS
;;
PATTERN2)
STATEMENTS
;;
*)
DEFAULT STATEMENTS
;;
esac
Example 1: Country Capitals
This script demonstrates a simple case statement that returns the capital city of a given country:
#!/bin/bash
echo -n "Enter the country name: "
read country
echo -n "Capital of $country is "
case $country in
"India")
echo "New Delhi"
;;
"Argentina")
echo "Buenos Aires"
;;
"Brazil")
echo "Brasília"
;;
*)
echo "unknown"
;;
esac
Example 2: System Administration Menu
Here’s a practical example for system administrators:
#!/bin/bash
echo "System Administration Menu"
echo "1. Check disk space"
echo "2. Check memory usage"
echo "3. Check CPU info"
echo "4. Exit"
read -p "Enter your choice (1-4): " choice
case $choice in
1)
df -h
;;
2)
free -m
;;
3)
lscpu
;;
4)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice"
;;
esac
Example 3: File Type Checker
This script uses case statement with pattern matching to identify file types:
#!/bin/bash
read -p "Enter filename: " filename
case $filename in
*.jpg|*.jpeg|*.png)
echo "Image file"
;;
*.txt|*.doc|*.pdf)
echo "Document file"
;;
*.mp3|*.wav)
echo "Audio file"
;;
*)
echo "Unknown file type"
;;
esac
Example 4: Day of Week
Using case with command substitution:
#!/bin/bash
day=$(date +%u)
case $day in
1)
echo "It's Monday - Start of the work week"
;;
2|3|4)
echo "It's a regular work day"
;;
5)
echo "It's Friday - Almost weekend!"
;;
6|7)
echo "It's weekend!"
;;
*)
echo "Invalid day number"
;;
esac
Advanced Features
Multiple Patterns
You can match multiple patterns using the |
operator:
case $answer in
y|Y|yes|YES)
echo "Proceeding..."
;;
n|N|no|NO)
echo "Aborting..."
;;
esac
Pattern Matching
Case supports pattern matching with wildcards:
case $filename in
*.txt)
echo "Text file"
;;
data???)
echo "Data file with 3 digits"
;;
[Ll]og*)
echo "Log file"
;;
esac
Best Practices
- Always include a default case (
*
) to handle unexpected inputs - Use double quotes around variables to prevent word splitting
- Keep related patterns together for better readability
- Add comments for complex pattern matching
- Use consistent indentation for better code structure
When to Use Case Statements
Case statements are particularly useful when:
- You have multiple conditions based on a single variable
- Pattern matching is needed
- Code readability is a priority
- You want to avoid complex nested if-else structures
Common Pitfalls
- Forgetting the
;;
terminator after each case - Missing the closing
esac
- Not handling the default case
- Using complex expressions that should be handled by if statements
See Also
Remember that while case statements are powerful, they’re best suited for simple pattern matching and equality comparisons. For more complex conditions, consider using if-else statements or other control structures.