让 bash 脚本回答交互式提示
是否可以让 bash 脚本自动处理通常通过默认操作呈现给用户的提示?目前,我正在使用 bash 脚本调用内部工具,该工具将向用户显示提示(提示是/否)以完成操作,但是我正在编写的脚本需要完全“不干涉”,所以我需要一种方法将 Y|N
发送到提示符以允许程序继续执行。这可能吗?
Is it possible to have a bash script automatically handle prompts that would normally be presented to the user with default actions? Currently I am using a bash script to call an in-house tool that will display prompts to the user (prompting for Y/N) to complete actions, however the script I'm writing needs to be completely "hands-off", so I need a way to send Y|N
to the prompt to allow the program to continue execution. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个简单的
这允许您将任何“Y”或“N”序列传递给您的脚本。
A simple
This allow you to pass any sequence of "Y" or "N" to your script.
这不是“自动完成”,这是自动化。用于这些事情的一种常用工具称为 Expect。
您也可以只使用
yes
的管道输入。This is not "auto-completion", this is automation. One common tool for these things is called Expect.
You might also get away with just piping input from
yes
.如果您只有 Y 要发送:
如果您只有 N 要发送:
If you only have Y to send :
If you only have N to send :
我发现发送输入的最佳方法是使用 cat 和文本文件来传递您需要的任何输入。
I found the best way to send input is to use cat and a text file to pass along whatever input you need.
为此有一个特殊的内置实用程序 - 'yes'。
要以相同的答案回答所有问题,您可以运行
或者您可以将其放入脚本中,对每个问题都有具体的答案
There is a special build-in util for this - 'yes'.
To answer all questions with the same answer, you can run
Or you can put it inside your script have specific answer to each question
在我的情况下,我需要回答一些没有 Y 或 N 但有文本或空白的问题。我发现在我的情况下执行此操作的最佳方法是创建一个 shellscript 文件。就我而言,我将其称为 autocomplete.sh
我需要回答学说模式导出器的一些问题,因此我的文件如下所示。
-- 这只是一个示例 --
我喜欢这个策略的一点是您可以评论您的答案,并且使用 EOF 空行就是这样(默认答案)。顺便说一下,这个导出器工具有自己的 JSON 对应项来回答这些问题,但我在执行此操作后发现了这一点 =)。
要运行脚本,只需在您想要的目录中并在终端中运行
'sh autocomplete.sh'
即可。简而言之,通过使用 <<停产& EOF 结合返回行,您可以根据需要回答提示的每个问题。 每一个新行都是一个新的答案。
我的示例只是展示了如何通过使用 ` 字符的注释来完成此操作,以便您记住每个步骤是什么。
注意这种方法的另一个优点是您可以回答更多的问题,而不仅仅是 Y 或 N ...事实上您可以回答空白!
希望这可以帮助别人。
In my situation I needed to answer some questions without Y or N but with text or blank. I found the best way to do this in my situation was to create a shellscript file. In my case I called it autocomplete.sh
I was needing to answer some questions for a doctrine schema exporter so my file looked like this.
-- This is an example only --
The thing I like about this strategy is you can comment what your answers are and using EOF a blank line is just that (the default answer). Turns out by the way this exporter tool has its own JSON counterpart for answering these questions, but I figured that out after I did this =).
to run the script simply be in the directory you want and run
'sh autocomplete.sh'
in terminal.In short by using << EOL & EOF in combination with Return Lines you can answer each question of the prompt as necessary. Each new line is a new answer.
My example just shows how this can be done with comments also using the ` character so you remember what each step is.
Note the other advantage of this method is you can answer with more then just Y or N ... in fact you can answer with blanks!
Hope this helps someone out.