Bash 脚本:循环执行后始终显示菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20224862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:46:45  来源:igfitidea点击:

Bash script: always show menu after loop execution

linuxbashunixloopsmenu

提问by EightBall

I'm using a bash script menu like this:

我正在使用这样的 bash 脚本菜单:

#!/bin/bash
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose choice 1"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

After each menu selection I just get prompted with

每次选择菜单后,我都会收到提示

Please enter your choice:

How do I always show the menu after each option has finished execution? I've done some looking around and I think I can do some kind of while loop, but I haven't been able to get anything working.

如何在每个选项完成执行后始终显示菜单?我环顾四周,我想我可以做某种 while 循环,但是我什么都没做。

回答by thom

Make it beautiful and userfriendly ;-)

使它美观和用户友好;-)

#!/bin/bash

while :
do
    clear
    cat<<EOF
    ==============================
    Menusystem experiment
    ------------------------------
    Please enter your choice:

    Option (1)
    Option (2)
    Option (3)
           (Q)uit
    ------------------------------
EOF
    read -n1 -s
    case "$REPLY" in
    "1")  echo "you chose choice 1" ;;
    "2")  echo "you chose choice 2" ;;
    "3")  echo "you chose choice 3" ;;
    "Q")  exit                      ;;
    "q")  echo "case sensitive!!"   ;; 
     * )  echo "invalid option"     ;;
    esac
    sleep 1
done

Replace the echos in this example with function calls or calls to other scripts.

用函数调用或对其他脚本的调用替换本例中的回声。

回答by Ranjithkumar T

just modified your script like below. its working for me !

刚刚修改了你的脚本,如下所示。它对我来说有效!

 #!/bin/bash
 while true
 do
 PS3='Please enter your choice: '
 options=("Option 1" "Option 2" "Option 3" "Quit")
 select opt in "${options[@]}" 
 do
     case $opt in
         "Option 1")
             echo "you chose choice 1"
             break
             ;;
         "Option 2")
             echo "you chose choice 2"
             break
             ;;
         "Option 3")
             echo "you chose choice 3"
             break
             ;;
         "Quit")
             echo "Thank You..."                 
             exit
             ;;
         *) echo invalid option;;
     esac
 done
 done

回答by Jay

You can also do something like:

您还可以执行以下操作:

#!/bin/bash

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose choice 1"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
    counter=1
    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")   # we set another delimiter, see also: https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
    for i in ${options[@]};
    do
        echo $counter')' $i
        let 'counter+=1'
    done
    IFS=$SAVEIFS
done