In this tutorial, we aim to provide a comprehensive and easy-to-understand guide on how to use case statements in shell scripting. Case statements are a powerful feature of shell scripting that allow you to test multiple possible values of a variable or expression in an organized and efficient manner.
By the end of this tutorial, you will learn how to:
- Understand and write case statements
- Use case statements effectively in your shell scripts
Prerequisites:
- Basic understanding of shell scripting
- Familiarity with shell command line
One of the important concepts in shell scripting is conditional statements. They help in controlling the flow of execution of the script. One such conditional statement is case
.
A case construct in shell scripting is similar to switch statement in C. It can be used to test simple values like integers and characters.
Here is the general form of a case statement:
case expression in
pattern1)
statements ;;
pattern2)
statements ;;
*)
statements ;;
esac
In the above structure, the case
statement checks the expression
against each pattern
from the top to the bottom. Once a pattern
matches, it executes the corresponding statements
. The ;;
indicates the end of each case. The *
pattern will match anything if no other pattern matches.
Let's consider a practical example.
#! /bin/bash
echo "Enter a character:"
read char
case $char in
[a-z])
echo "You entered a lower case alphabet." ;;
[A-Z])
echo "You entered an upper case alphabet." ;;
[0-9])
echo "You entered a digit." ;;
?)
echo "You entered a special character." ;;
*)
echo "Invalid input!" ;;
esac
In this script, we are reading a character from the user. The case statement then checks the character against each pattern. The first pattern [a-z]
checks if the character is a lower case alphabet, and so on. The ?
pattern checks for any single character.
If you run this script and input 'a', the output will be "You entered a lower case alphabet."
We covered how to use case statements in shell scripting, including writing case statements and understanding their flow of execution. The next step would be to practice using case statements in more complex scripts. You may also want to explore other types of conditional statements in shell scripting.
Write a script that takes a day of the week (1-7) from the user and prints the corresponding day name (1 for Sunday, 2 for Monday, etc.). If the input is not between 1 and 7, print an error message.
Modify the above script to use the day name instead of the day number. If the input is not a valid day name, print an error message.
Solutions
#! /bin/bash
echo "Enter a number (1-7):"
read day
case $day in
1)
echo "Sunday" ;;
2)
echo "Monday" ;;
3)
echo "Tuesday" ;;
4)
echo "Wednesday" ;;
5)
echo "Thursday" ;;
6)
echo "Friday" ;;
7)
echo "Saturday" ;;
*)
echo "Invalid input! Please enter a number between 1 and 7." ;;
esac
#! /bin/bash
echo "Enter a day:"
read day
case $day in
"Sunday"|"sunday")
echo "1" ;;
"Monday"|"monday")
echo "2" ;;
"Tuesday"|"tuesday")
echo "3" ;;
"Wednesday"|"wednesday")
echo "4" ;;
"Thursday"|"thursday")
echo "5" ;;
"Friday"|"friday")
echo "6" ;;
"Saturday"|"saturday")
echo "7" ;;
*)
echo "Invalid input! Please enter a valid day." ;;
esac
Remember, practice is key when it comes to mastering new concepts. Happy scripting!