Welcome to our article on how to create a nicER menu terminal to improve your Bash skills. In this article, we will explore the concept of function menus in Bash and learn how to hardcode values while allowing for a varying number of options. Let's dive right in!
What is a Function Menu in Bash?
In Bash, a function menu is a script that provides options to users through a menu format. Function menus can greatly improve the user experience by simplifying the process of executing various commands, scripts, or tasks. They can also make it easier to manage the complexity of a script by organizing its functionality into smaller, more manageable pieces.
Creating a Basic Function Menu
To create a basic function menu, you can define a function that uses the echo and read commands to display a list of options and capture the user's input. Here's an example:
function menu() {
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo -n "Please enter your choice: "
read choice
}
In this example, the user can enter the number corresponding to the desired option, and the read command will store the user's input in the choice variable. However, this basic function menu has limitations. For instance, it only supports a fixed set of options, and it doesn't handle input validation or error handling.
Improving the Function Menu: Dynamic Options and Input Validation
To create a more robust function menu, you can improve the script by:
- Making the number of options dynamic
- Validating the user's input
- Handling errors when the user enters invalid input
To achieve this, you can create an associative array with keys as option numbers and values as command strings. Here's an example:
declare -A options
options=( ["1"]="command1" ["2"]="command2" ["3"]="command3" )
function menu() {
local i=1
for key in "${!options[@]}"; do
echo "${i}. ${key}"
i=$((i+1))
done
echo -n "Please enter your choice: "
read choice
if [[ -z "${options[$choice]}" ]]; then
echo "Invalid choice, please try again."
menu
else
${options[$choice]}
fi
}
In this example, the number of options is dynamic, as it relies on the associative array with keys and commands. The script also validates the input, ensuring the choice corresponds to a valid option number. If the user enters an invalid option number, the script will display an error message and call the menu function again.
NicER Menu Terminal: Enhancing the Look and Feel
While the examples above create a functional menu, they don't provide the best user experience. To improve the look and feel of the menu, you can:
- Add colors and formatting
- Clear the screen before displaying the menu
- Incorporate better error message handling
The following example demonstrates how to create a nicER menu terminal:
function nicermenu() {
clear
local i=1
local red="\033[1;31m"
local green="\033[1;32m"
local yellow="\033[1;33m"
local reset="\033[0m"
echo -e "${red}Welcome to the nicER terminal menu${reset}"
echo -e "${yellow}----------------------------------${reset}"
for key in "${!options[@]}"; do
echo "${i}. ${key}"
i=$((i+1))
done
echo -n "Please enter your choice: "
read choice
if [[ -z "${options[$choice]}" ]]; then
echo -e "${red}Invalid choice, please try again.${reset}"
sleep 1
nicermenu
else
${options[$choice]}
fi
}
This example adds formatting and colors to the menu and includes a sleep 1 command to pause before redisplaying the menu after an error message. This creates a more polished, user-friendly experience when navigating the menu.
- Function menus in Bash simplify the user experience and help manage script complexity.
- You can create dynamic function menus by using an associative array for options and input validation.
- Improve the look and feel of your menu terminal by incorporating colors, clea
- Improve the look and feel of your menu terminal by incorporating colors, clearing the screen, and handling errors with a better user experience.
References
- Associative Arrays in Bash
- Bash Conditional Expressions
- Colorizing Bash Scripts
- Linux Command Line and Shell Scripting Bible by Christopher Negus