【问题标题】:How to create a menu with a range of options?如何创建具有一系列选项的菜单?
【发布时间】:2018-02-11 12:18:50
【问题描述】:

我想在 shell 脚本中创建一个带有一系列选项的菜单。在这种情况下,我想强制用户只使用一个数字范围,例如1 到 5,但不使用 CASE。如果用户选择 6,菜单会再次询问范围之间的数字。

我记得类似:

OPTION (){
    [[ $option = +(1|2|3|4|5) ]] || OPTION
}

你能帮帮我吗? 谢谢!

【问题讨论】:

    标签: linux bash menu option choice


    【解决方案1】:

    以下内容可能对您有所帮助:

    cat choose2.ksh
    check() {
    while [ ! ${finished} ]
    do
    echo "Please enter a digit:"
    read value
    if [[ $value -le 5 ]]
    then
            echo "user entered between 1 to 5."
            finished=1
    else
            echo "user entered more than 5 in this case."
    fi
    done
    }
    
    check
    

    脚本的执行:

    ./choose2.ksh
    Please enter a digit:
    12
    user entered more than 5 in this case.
    Please enter a digit:
    12
    user entered more than 5 in this case.
    Please enter a digit:
    1
    user entered between 1 to 5.
    

    因此您可以看到,如果用户输入的不是 1 到 5,那么它会再次询问用户输入,否则它将简单地从脚本中出来(您也可以根据需要做其他事情)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多