输入菜单选项后,我的bash菜单不会返回...这是我的代码

时间:2019-02-28 03:29:55

标签: bash

不知道我在这里做错了什么,但我需要循环菜单并且仅在退出时退出。我尝试了很多选项,但它只会弄乱我的代码和菜单。任何帮助和/或想法都将不胜感激。

#!/bin/bash
# A menu driven shell script sample template
## ----------------------------------
# Step #1: Define variables
# ----------------------------------


EDITOR=vim
PASSWD=/etc/passwd
RED='\033[0;41;30m'
STD='\033[0;0;39m'
COLUMNS=12

# this function is called when Ctrl-C is sent
function trap_ctrlc ()
{
    # perform cleanup here
    echo "Ctrl-C EXITING MENU"

    # exit shell script with error code 2
    # if omitted, shell script will continue execution
    exit 2
}

# initialise trap to call trap_ctrlc function
# when signal 2 (SIGINT) is received
trap "trap_ctrlc" 2

# function to display menus
show_menus() {
    clear
	echo -e " \e[35m"
	cat << "EOF"
    /\   |  __ \ / ____| |  | | | |    |_   _| \ | | |  | \ \ / / |__   __/ __ \ / __ \| |     / ____|
   /  \  | |__) | |    | |__| | | |      | | |  \| | |  | |\ V /     | | | |  | | |  | | |    | (___
  / /\ \ |  _  /| |    |  __  | | |      | | | . ` | |  | | > <      | | | |  | | |  | | |     \___ \
 / ____ \| | \ \| |____| |  | | | |____ _| |_| |\  | |__| |/ . \     | | | |__| | |__| | |____ ____) |
/_/    \_\_|  \_\\_____|_|  |_| |______|_____|_| \_|\____//_/ \_\    |_|  \____/ \____/|______|_____/
EOF
	echo
	echo -e "\e[1m\e[31mTools Version v.1.4.1 \e[0m" # Tools Version
	echo -e "\e[35m \e[1m"
    echo "+++++++++++++++++++++++++++++++"
    echo -e "\e[1m\e[36mBash Version | $BASH_VERSION"
    now=$(date +"%r")
    echo -e "\e[36m\e[1mCurrent Time | $now"
    echo -e "\e[35m\e[1m+++++++++++++++++++++++++++++++"
	echo -e "\e[35m \e[1m"
    # UNDERLINE
    echo -e "+++++++++++++++++++++++++++++++"
    echo -e "\e[40;38;5;82m A R C H \e[30;48;5;82m T O O L S  M E N U \e[0m"
    echo -e "\e[35m\e[1m+++++++++++++++++++++++++++++++"
	echo -e "\e[35m \e[1m"
    echo '  '"1. Calendar"
    echo '  '"2. Speedtest"
    echo '  '"3. List Hardware (lshwd)"
    echo '  '"4. Kernel Version"
    echo '  '"5. Free Memory"
    echo '  '"6. System Startup Time"
    echo '  '"7. Package Manager (Pamac Update)"
    echo '  '"8. Package Manager (Yay Update)"
    echo '  '"9. List Packages (Yaourt)"
	echo '  '"10. SSH Config"
	echo '  '"11. NGINX Config"
	echo '  '"12. Apache Config (httpd)"
	echo '  '"13. PHP Config (php.ini)"
	echo '  '"14. PHP-FPM (php.fpm.conf)"
	echo '  '"15. Samba Config (smb.conf)"
	echo '  '"16. Squid Config (squid.conf)"
	echo '  '"17. Privoxy Config"
	echo '  '"18. Display Network Config"
    echo '  '"19. Get LAN IP"
    echo '  '"20. UpdateDB"
    echo '  '"21. Exit"
}

# read input from the keyboard and take a action
# invoke the one() when the user select 1 from the menu option.
# invoke the two() when the user select 2 from the menu option.
# Exit when user the user select 3 form the menu option.
read_options(){
    local choice
    read -p "Enter choice [ 1 - 21] " choice
    case $choice in
        1) exec cal ; break ;;
        2) exec speedtest-cli ; break ;;
        3) exec lshwd ; break ;;
        4) exec uname -r ; break ;;
        5) exec free -m ; break ;;
        6) exec systemd-analyze ;;
        7) exec pamac update ;;
        8) exec yay ;;
        9) exec yaourt -Q ;;
		10) exec sudo xed /etc/ssh/sshd_config ;;
		11) exec sudo xed /etc/nginx/nginx.conf ;;
		12) exec sudo xed /etc/httpd/conf/httpd.conf ;;
		13) exec sudo xed /etc/php/php.ini ;;
		14) exec sudo xed /etc/php/php-fpm.conf ;;
		15) exec sudo xed /etc/samba/smb.conf ;;
		16) exec sudo xed /etc/squid/squid.conf ;;
		17) exec sudo xed /etc/privoxy/config ;;
		18) exec netstat -nat ;;
        19) exec wanip ;;
        20) exec sudo updatedb ;;
        21) exit 0 ;;
        *) echo -e "${RED}Error... Invalid option${STD}" && sleep 2 ;;
    esac
}

# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while : ; do
	clear

    show_menus
    read_options
return
done

不知道我在这里做错了什么,但我需要循环菜单并且仅在退出时退出。我尝试了很多选项,但它只会弄乱我的代码和菜单。任何帮助和/或想法都将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是input[value="0000-00-00"]::-webkit-datetime-edit { color: transparent; } 使用read_options()运行选定的程序。 exec命令用您指定的程序替换了shell进程,因此之后,shell脚本中没有其他运行。您应该只正常执行命令,而不使用exec

此外,您不应使用exec,因为它会终止break循环。

因此,您的while语句应如下所示:

case