Linux 让 bash 脚本回答交互式提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3804577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:38:44  来源:igfitidea点击:

Have bash script answer interactive prompts

linuxbashautomationcommand-promptprompt

提问by TJ L

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|Nto the prompt to allow the program to continue execution. Is this possible?

是否可以让 bash 脚本自动处理通常会以默认操作呈现给用户的提示?目前我正在使用 bash 脚本调用一个内部工具,该工具将向用户显示提示(提示 Y/N)以完成操作,但是我正在编写的脚本需要完全“不干涉”,所以我需要一种方法来发送Y|N到提示以允许程序继续执行。这可能吗?

采纳答案by unwind

This is not "auto-completion", this is automation. One common tool for these things is called Expect.

这不是“自动完成”,这是自动化。这些事情的一种常用工具称为Expect

You might also get away with just piping input from yes.

您也可以仅通过管道输入从yes.

回答by Lo?c Février

A simple

一个简单的

echo "Y Y N N Y N Y Y N" | ./your_script

This allow you to pass any sequence of "Y" or "N" to your script.

这允许您将任何“Y”或“N”序列传递给您的脚本。

回答by Alex

I found the best way to send input is to use cat and a text file to pass along whatever input you need.

我发现发送输入的最佳方式是使用 cat 和一个文本文件来传递您需要的任何输入。

cat "input.txt" | ./Script.sh

回答by douardo

If you only have Y to send :

如果您只有 Y 要发送:

$> yes Y |./your_script

If you only have N to send :

如果您只有 N 个要发送:

$> yes N |./your_script

回答by Joseph Astrahan

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

在我的情况下,我需要回答一些没有 Y 或 N 但有文本或空白的问题。我发现在我的情况下最好的方法是创建一个 shellscript 文件。就我而言,我称它为 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--

--这只是一个例子--

php vendor/bin/mysql-workbench-schema-export mysqlworkbenchfile.mwb ./doctrine << EOF
`#Export to Doctrine Annotation Format`                                     1
`#Would you like to change the setup configuration before exporting`        y
`#Log to console`                                                           y
`#Log file`                                                                 testing.log
`#Filename [%entity%.%extension%]`
`#Indentation [4]`
`#Use tabs [no]`
`#Eol delimeter (win, unix) [win]`
`#Backup existing file [yes]`
`#Add generator info as comment [yes]`
`#Skip plural name checking [no]`
`#Use logged storage [no]`
`#Sort tables and views [yes]`
`#Export only table categorized []`
`#Enhance many to many detection [yes]`
`#Skip many to many tables [yes]`
`#Bundle namespace []`
`#Entity namespace []`
`#Repository namespace []`
`#Use automatic repository [yes]`
`#Skip column with relation [no]`
`#Related var name format [%name%%related%]`
`#Nullable attribute (auto, always) [auto]`
`#Generated value strategy (auto, identity, sequence, table, none) [auto]`
`#Default cascade (persist, remove, detach, merge, all, refresh, ) [no]`
`#Use annotation prefix [ORM\]`
`#Skip getter and setter [no]`
`#Generate entity serialization [yes]`
`#Generate extendable entity [no]`                                          y
`#Quote identifier strategy (auto, always, none) [auto]`
`#Extends class []`
`#Property typehint [no]`
EOF

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 =).

我喜欢这个策略的一点是你可以评论你的答案是什么,并且使用 EOF 一个空行就是这样(默认答案)。事实证明,这个导出器工具有自己的 JSON 对应物来回答这些问题,但我在这样做后发现了这一点 =)。

to run the script simply be in the directory you want and run 'sh autocomplete.sh'in terminal.

运行脚本只需在您想要的目录中并'sh autocomplete.sh'在终端中运行。

In short by using << EOL & EOFin combination with Return Linesyou can answer each question of the prompt as necessary. Each new line is a new answer.

简而言之,通过将<< EOL & EOF与 Return Lines 结合使用,您可以根据需要回答提示中的每个问题。 每一个新行都是一个新答案。

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!

请注意,此方法的另一个优点是您可以用更多而不是 Y 或 N 来回答……事实上,您可以用空格来回答!

Hope this helps someone out.

希望这可以帮助某人。

回答by Asaf Pinhassi

There is a special build-in util for this - 'yes'.

有一个特殊的内置实用程序 - ''。

To answer all questions with the same answer, you can run

要以相同的答案回答所有问题,您可以运行

yes [answer] |./your_script

Or you can put it inside your script have specific answer to each question

或者你可以把它放在你的脚本中,每个问题都有特定的答案