I’m diving into a little project on my Ubuntu machine, and I’ve hit a bit of a snag with my shell script. I’m trying to create a simple selection menu that lets users choose from a list of options when they run the script. You know, something that’s interactive and user-friendly, but I’m not sure how to implement it properly!
I want the menu to display a few options, and then based on the user’s input, it should perform a specific action. For example, I’m thinking of giving the options like “1. Check Disk Usage”, “2. List Installed Packages”, and “3. Exit”. When they select an option by pressing a corresponding number and hit enter, the script should then execute the relevant command. Sounds straightforward, right?
The thing is, my scripting skills are a bit rusty, and I’m worried my menu might end up being clunky or non-functional. I’ve read somewhere that using a case statement could be a good way to handle the user input, but I’m not entirely sure how to structure everything. I can imagine it could look something like:
“`bash
echo “Choose an option:”
echo “1. Check Disk Usage”
echo “2. List Installed Packages”
echo “3. Exit”
read -p “Enter your choice: ” choice
case $choice in
1)
# Command to check disk usage
;;
2)
# Command to list installed packages
;;
3)
echo “Goodbye!”
exit 0
;;
*)
echo “Invalid option, please try again.”
;;
esac
“`
But I have a feeling there’s more I could do to make it more polished. Also, how do I handle situations where the user inputs something unexpected?
I guess I’m really looking for any tips or examples from anyone who has tackled something similar. Have you implemented a menu like this before? What are the best practices for making it clean and user-friendly? Any code snippets or suggestions would be super helpful! Thanks in advance!
Creating an interactive selection menu in a shell script can greatly enhance user experience. The `echo` commands you already have will display options effectively, and using the `case` statement is a clean way to handle the user input. To structure your script, you can refine the commands within each case to make it more functional. For example, to check disk usage, you can use the `df -h` command, and to list installed packages, you might use `dpkg –get-selections`. Additionally, to improve user experience, consider wrapping your script in a loop that repeatedly displays the menu until the user chooses to exit. This allows users to perform multiple actions without restarting the script each time.
To handle unexpected inputs gracefully, you already have the default case that prompts the user if they provide an invalid option. Consider adding a validation loop which continues to ask for input until a valid response is given. You might also want to incorporate a helpful message that explains the valid options before asking for input. Below is a refactored version of your script that includes these improvements:
Creating an Interactive Menu in a Shell Script
It sounds like you’re on the right track with your shell script, and using a case statement is definitely a great way to handle user input in a clear way! Here’s how you could structure your script to make it interactive and user-friendly:
A few tips to polish it up:
while true
loop so that the menu keeps showing until the user exits. This is handy if they want to run another command after one has finished.*
case you already have will manage that by asking the user to try again.Running commands like
df -h
for disk usage anddpkg --get-selections
for installed packages will give users useful information while keeping everything inside your menu. The script can always be expanded with more options later, making it flexible!Give this a go, and you should have a nice, user-friendly menu up and running in no time!