Bash脚本,让用户从列表中选择或输入自己的 [英] Bash Script to let user choose from a list or enter their own
问题描述
我有一个简单的脚本,我想列出一些预先填写的选项,或者让用户输入一个与构建相关的数字
I have a simple script which i want to list some pre-filled choices or for the user to enter a number to correlate with a build
这是我目前所拥有的.
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
问题是没有提供供用户选择的列表.
The problem is that is doesn't provide a list for the user to choose.
理想情况下看起来像这样
Ideally it would look something like this
构建选择1)最新3)成功5)选择内部版本号(1-10000)2)稳定4)等请从上面的列表中选择或输入您要下载的内部版本号:_
Build Choices 1) Latest 3) Successful 5) pick a build number (1-10000) 2) Stable 4) etc. Please select from the list above or enter the build number you would like to download: _
还是最好在2个case语句中执行此操作?询问他们是否要选择特定的版本或输入自己的版本.
Or would it be better to do this in 2 case statements? Asking if they want to choose a particular build or enter their own.
更新:经过一番思考,我最终将其简化为是"或否"声明
UPDATE: After some thinking I ended up simplifying it to a yes no statement
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
语句可能就是您想要的:
A select
statement might be what you want:
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
设置为相应的字符串.
The user can enter 1, 2, or 3 and the variable name
is set to the corresponding string.
这篇关于Bash脚本,让用户从列表中选择或输入自己的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!