Linux中如何退出select
我想从用户那里获取输入,这可以是任何给定的选项,我尝试使用 select,但它只是循环,没有出来,有什么办法可以使其在用户输入正确的选项后出现并继续,这是示例代码:
select envir in "prod" "uat" "dev"; do
echo $envir
done
//continue with the script
echo "out of select"
一旦用户选择任何可用选项,它应该出现并继续执行脚本,如果用户输入了其他任何内容,它应该继续提示。
I want to take the input from the user, which could be any of the given options, I tried using select, but it just goes in loop, doesn't come out, is there a way i can make it to come out and proceed after the user has entered right option, here is the sample code:
select envir in "prod" "uat" "dev"; do
echo $envir
done
//continue with the script
echo "out of select"
Once user selects any of the available options, it should come out and continue with the scripts, if the user has entered anything else, it should keep on prompting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 bash(1) 手册页:
...列表被执行
每次选择后,直到执行中断命令。 select的退出状态是
列表中执行的最后一个命令的退出状态,如果没有命令被执行则为零
可爱的。 ...
换句话说,当 $envir 非空时执行“break”语句。
From the bash(1) man page:
... The list is executed
after each selection until a break command is executed. The exit status of select is
the exit status of the last command executed in list, or zero if no commands were exe-
cuted. ...
In other words, execute a "break" statement when $envir is nonempty.
谢谢布莱恩。根据您的输入,这就是我能够做的:
Thanks Brian. With your input, this is what I was able to do:
我会这样编写上面的脚本:
这样您的关键字就可以在一个地方处理。每次在“prod”“uat”“dev”列表中添加新单词时,不需要在2个地方进行更改。
您还可以从外部文件读取单词列表并将其分配给此处的 bash 数组变量 opts。
I would write the above script this way:
That way your keywords are handled at one place. Every time you add a new word in the list of "prod" "uat" "dev", you don't need to change at 2 places.
You can also read the list of words from an external file and assign that to bash array variable opts here.
在我看来,这些答案中应该有一个 case 语句的例子,因为将它们与 select 语句配对是很常见的。
There should be an example of a case-statement among these answers IMO, because it is very common to pair them with select-statements.