因此,我创建了一个菜单,这样我就可以控制我的 VPN 应用程序,而无需一次又一次输入命令。
但是,我希望能够选择数字 7,按 Enter 键,然后在菜单中手动输入命令。当命令执行时,我希望它返回到菜单。
我已经尝试过读取命令,但到目前为止还没有运气。
谢谢,
编辑 - 重新利用我从 DevDojo 获取的代码。根据要求:
#!/bin/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} is: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name} is: "
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} is: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
##
# Color Variables
##
green='\e[32m'
blue='\e[34m'
clear='\e[0m'
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
menu(){
echo -ne "
My First Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') Number of TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
read 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 $red"Wrong option."$clear; WrongCommand;;
esac
}
# Call the menu function
menu
答案1
你可以做类似的事情
read -p 'Cmd? ' cmd
eval $cmd