让 bash 脚本回答交互式提示

发布于 2024-09-25 02:15:50 字数 157 浏览 7 评论 0原文

是否可以让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半﹌身腐败 2024-10-02 02:15:50

一个简单的

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

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

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.

只是我以为 2024-10-02 02:15:50

这不是“自动完成”,这是自动化。用于这些事情的一种常用工具称为 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.

玩物 2024-10-02 02:15:50

如果您只有 Y 要发送:

gt; yes Y |./your_script

如果您只有 N 要发送:

gt; yes N |./your_script

If you only have Y to send :

gt; yes Y |./your_script

If you only have N to send :

gt; yes N |./your_script
孤独岁月 2024-10-02 02:15:50

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

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

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

cat "input.txt" | ./Script.sh
匿名。 2024-10-02 02:15:50

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

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

yes [answer] |./your_script

或者您可以将其放入脚本中,对每个问题都有具体的答案

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

渔村楼浪 2024-10-02 02:15:50

在我的情况下,我需要回答一些没有 Y 或 N 但有文本或空白的问题。我发现在我的情况下执行此操作的最佳方法是创建一个 shellscript 文件。就我而言,我将其称为 autocomplete.sh

我需要回答学说模式导出器的一些问题,因此我的文件如下所示。

-- 这只是一个示例 --

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

我喜欢这个策略的一点是您可以评论您的答案,并且使用 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 --

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文