Bash 脚本:循环执行后始终显示菜单 [英] Bash script: always show menu after loop execution

查看:19
本文介绍了Bash 脚本:循环执行后始终显示菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a bash script menu like this:

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

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

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.

推荐答案

让它变得美观和用户友好 ;-)

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.

这篇关于Bash 脚本:循环执行后始终显示菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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