Bash 让用户从列表中选择或输入自己的脚本

Bash Script to let user choose from a list or enter their own

我有一个简单的脚本,我想列出一些预填充的选项或让用户输入一个数字以与构建相关联

这是我目前拥有的。

read -p "Please select from the list below or enter the build number you would like to download " build
    case {build} in
        Latest)
            build=lastSuccessful
            break;;
        *)
            break;;
    esac

问题是没有提供列表供用户选择。

理想情况下它看起来像这样

构建选择 1) 最新 3) 成功 5) 选择内部版本号 (1-10000) 2) 稳定 4) 等 请从上面的列表中 select 或输入您要下载的内部版本号:_

或者在 2 个 case 语句中执行此操作会更好吗?询问他们是要选择特定版本还是输入自己的版本。

更新:经过一番思考,我最终将其简化为是非声明

while true;do
    read -p "Would you like to download the latest successful build? (yes/no) " yn
    case $yn in
        [Yy]*)
            echo
            build=lastSuccessfulBuild
            break;;
        [Nn]*)
            echo
            read -p "Enter the build number to download: " build
            break;;
        *) echo 'I am not going to tell you again';;
        [Qq]*) exit 0;;
    esac
done

select 语句可能就是您想要的:

select name in Latest Successful  Stable "Pick a Build Number" ;
do
  case "$name" in
        Latest)
            build=lastSuccessful
            break
          ;;
        Successful)
            build=lastSuccessful
            break
          ;;
        Stable)
            build=lastSuccessful
            break
            ;;
        Pick*)
            read -p "Enter a number from 1 to 10000: " number
            break
            ;;
  esac
done

这会生成菜单:

1) Latest
2) Successful
3) Stable
4) Pick a Build Number
#?

用户可以输入 1、2 或 3,变量 name 设置为相应的字符串。

你可能也会喜欢dialog

看看这个:

dialog --no-cancel --menu "Please select from the list below or enter the build number you would like to download " 11 60 12 1 Latest 2 Successful 3 Stable