Files
linux.softwareshinobi.com/landing/docs/Bash-Scripts/016-creating-an-interactive-menu-in-bash.md

266 lines
5.2 KiB
Markdown
Raw Normal View History

2025-06-19 10:03:08 -04:00
# Interactive Menu
Build interactive Bash menus. This guide shows you how to create a menu where users choose which actions to run, leveraging previously defined functions for server status checks:
* Memory usage
* CPU load
* TCP connections
* Kernel version
Here's the base script containing these functions:
```bash
#!/usr/bin/env bash
##
# Server status script:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name}: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name}: "
echo ""
uptime
echo ""
}
function tcp_check() {
echo ""
echo "TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo ""
}
function kernel_check() {
echo ""
echo "Kernel version on ${server_name}: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
```
We'll now integrate this with a menu, allowing users to select a function to execute.
---
### Add Color for Readability
Enhance your menu's readability with simple color functions. Add these variables and functions to your script:
```bash
##
# Color Variables
##
green='\e[32m'
blue='\e[34m'
red='\e[31m'
clear='\e[0m'
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
ColorRed(){
echo -ne $red$1$clear
}
```
Use them like: `$(ColorBlue 'Your text')`.
---
### Build the Interactive Menu
Create a `menu` function containing the display logic, user input, and a `case` statement for selection.
```bash
menu(){
echo -ne "
My Server Status Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
read -r a
case $a in
1) memory_check ; menu ;;
2) cpu_check ; menu ;;
3) tcp_check ; menu ;;
4) kernel_check ; menu ;;
5) all_checks ; menu ;;
0) exit 0 ;;
*) echo -e $(ColorRed 'Invalid option.') ; menu ;;
esac
}
```
Here's how it works:
* The `echo -ne` block prints the menu options, applying colors.
* `read -r a` captures user input into variable `a`.
* The `case` statement executes the corresponding function. After each function, `menu` is called again to display the menu for another choice, creating a loop. `exit 0` handles the exit option.
Finally, call the `menu` function at the end of your script to start the interaction:
```bash
menu
```
---
### Complete Script and Testing
Here's the entire script, `menu.sh`, combining all components:
```bash
#!/usr/bin/env bash
##
# BASH menu script that checks:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name}: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name}: "
echo ""
uptime
echo ""
}
function tcp_check() {
echo ""
echo "TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo ""
}
function kernel_check() {
echo ""
echo "Kernel version on ${server_name}: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
##
# Color Variables
##
green='\e[32m'
blue='\e[34m'
red='\e[31m'
clear='\e[0m'
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
ColorRed(){
echo -ne $red$1$clear
}
menu(){
echo -ne "
My Server Status Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
read -r a
case $a in
1) memory_check ; menu ;;
2) cpu_check ; menu ;;
3) tcp_check ; menu ;;
4) kernel_check ; menu ;;
5) all_checks ; menu ;;
0) exit 0 ;;
*) echo -e $(ColorRed 'Invalid option.') ; menu ;;
esac
}
# Call the menu function
menu
```
Save this code as `menu.sh`:
```bash
touch menu.sh
```
Open `menu.sh` and paste the script. Save and close. Then, make it executable and run:
```bash
chmod +x menu.sh
./menu.sh
```
You'll see the colored menu, prompting for input. Select an option (e.g., `1` for memory check), and the corresponding information will display before the menu reappears, ready for another choice.
---
### Conclusion
You've built an interactive Bash menu, allowing users to effortlessly navigate script functionalities. This enhances user experience for your automation tools.
> {notice} This content was inspired by a piece by softwareshinobi on Dev Team Six.