Displaying a Menu and Getting User Input in a Shell Script
Displaying a menu and getting user input in a shell script is a common task that allows you to create interactive and user-friendly scripts. This technique is particularly useful when you want to present the user with a set of options and let them choose the desired action.
Creating a Simple Menu
Here's a basic example of how to display a menu and get user input in a shell script:
#!/bin/bash
# Display the menu
echo "Welcome to the Menu!"
echo "Please select an option:"
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Exit"
# Get user input
read -p "Enter your choice (1-4): " choice
# Handle the user's choice
case $choice in
1)
echo "You selected Option 1"
;;
2)
echo "You selected Option 2"
;;
3)
echo "You selected Option 3"
;;
4)
echo "Exiting the script..."
exit 0
;;
*)
echo "Invalid choice. Please try again."
;;
esac
In this example, the script first displays a menu with four options. It then uses the read
command to prompt the user for input and store their choice in the choice
variable. The case
statement is used to handle the user's choice and execute the corresponding action.
Enhancing the Menu with Mermaid Diagrams
To better understand the flow of the script, let's visualize it using a Mermaid diagram:
This diagram illustrates the flow of the script, showing how the menu is displayed, user input is gathered, and the appropriate action is taken based on the user's choice.
Handling User Input Validation
In the previous example, we simply used the read
command to get user input. However, it's often a good idea to validate the user's input to ensure that it's within the expected range of options. Here's an example that demonstrates how to validate the user's choice:
#!/bin/bash
while true; do
# Display the menu
echo "Welcome to the Menu!"
echo "Please select an option:"
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Exit"
# Get user input
read -p "Enter your choice (1-4): " choice
# Validate the user's choice
case $choice in
1|2|3)
echo "You selected Option $choice"
break
;;
4)
echo "Exiting the script..."
exit 0
;;
*)
echo "Invalid choice. Please try again."
;;
esac
done
In this example, we use a while
loop to continuously display the menu and get user input until a valid choice is made. The case
statement checks the user's input and handles the appropriate action. If the user enters an invalid choice, the script will prompt them to try again.
Real-Life Example: Ordering Food
Let's consider a real-life example to make this more relatable. Imagine you're creating a shell script to help a restaurant customer order their meal. Here's how you could implement the menu and user input:
#!/bin/bash
echo "Welcome to the Restaurant Menu!"
while true; do
echo "Please select your meal:"
echo "1. Burger ($10)"
echo "2. Pizza ($15)"
echo "3. Salad ($8)"
echo "4. Exit"
read -p "Enter your choice (1-4): " choice
case $choice in
1)
echo "You ordered a Burger for $10."
break
;;
2)
echo "You ordered a Pizza for $15."
break
;;
3)
echo "You ordered a Salad for $8."
break
;;
4)
echo "Thank you for visiting our restaurant. Goodbye!"
exit 0
;;
*)
echo "Invalid choice. Please try again."
;;
esac
done
In this example, the script displays a menu of food options with their respective prices. The user can then select their choice, and the script will confirm their order. If the user enters an invalid choice, the script will prompt them to try again.
By using a menu-driven approach and validating user input, you can create more user-friendly and interactive shell scripts that cater to the needs of your users.