输入菜单选项后,我的bash菜单不会返回...这是我的代码 [英] My bash menu won't return after entering a menu option... here's my code

查看:37
本文介绍了输入菜单选项后,我的bash菜单不会返回...这是我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Not sure what i'm doing wrong here but I need the menu to loop and only escape when quitting. Iv'e tried lots of options and it just keeps messing up my code and the menu. Any help and / or ideas are greatly appreciated.

#!/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

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

Not sure what i'm doing wrong here but I need the menu to loop and only escape when quitting. Iv'e tried lots of options and it just keeps messing up my code and the menu. Any help and / or ideas are greatly appreciated.

推荐答案

问题是 read_options()使用 exec 来运行选定的程序. exec 命令用您指定的程序替换shell进程,因此之后,shell脚本中没有其他程序可以运行.您应该只正常执行命令,而不使用 exec .

The problem is that read_options() uses exec to run the program that's selected. The exec command replaces the shell process with the program you specify, so nothing else in the shell script runs after that. You should just execute the commands normally, without using exec.

此外,您不应该使用 break ,因为这会终止 while 循环.

Also, you shouldn't use break, as that terminates the while loop.

因此,您的 case 语句应类似于:

So your case statement should look like:

case $choice in
    1) cal ;;
    2) speedtest-cli ;;
    3) lshwd ;;
    ...
    21) exit 0 ;;
    *) echo -e "${RED}Error... Invalid option${STD}" && sleep 2 ;;
esac

这篇关于输入菜单选项后,我的bash菜单不会返回...这是我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆