如何编写“是”的脚本安装程序的响应?

发布于 2024-12-08 02:31:19 字数 295 浏览 0 评论 0原文

我使用 Amazon Linux 实例,并且我有几个脚本来填充数据并安装所有我使用的程序,但有几个程序询问:

Do you want to continue [Y/n]?

并暂停安装。我想在所有情况下自动回答“Y”,我现在知道该怎么做。

I work with Amazon Linux instances and I have a couple scripts to populate data and install all the programs I work with, but a couple of the programs ask:

Do you want to continue [Y/n]?

and pause the install. I want to auto answer "Y" in all cases, I'm just now sure how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

无所的.畏惧 2024-12-15 02:31:19

'yes' 命令 将回显 'y' (或您要求的任何内容)无限期地。使用它:

yes | command-that-asks-for-input

或者,如果需要大写“Y”:

yes Y | command-that-asks-for-input

如果您想传递“N”,您仍然可以使用 yes

yes N | command-that-asks-for-input

The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. Use it as:

yes | command-that-asks-for-input

or, if a capital 'Y' is required:

yes Y | command-that-asks-for-input

If you want to pass 'N' you can still use yes:

yes N | command-that-asks-for-input
作业与我同在 2024-12-15 02:31:19

回声 y |命令应该可以工作。

此外,一些安装程序有一个“自动是”标志。 Ubuntu 上的 apt-get-y

echo y | command should work.

Also, some installers have an "auto-yes" flag. It's -y for apt-get on Ubuntu.

我做我的改变 2024-12-15 02:31:19

您可能无法在目标服务器上安装 Expect。
当人们编写 Jenkins 作业时,通常会出现这种情况。

如果是这样,我会考虑在askubuntu.com上回答以下问题:

https: //askubuntu.com/questions/338857/automatically-enter-input-in-command-line

printf 'y\nyes\nno\nmaybe\n' | ./script_that_needs_user_input

请注意,在极少数情况下,该命令不需要用户在字符后按 Enter 键。在这种情况下,请保留换行符:

printf 'yyy' | ./script_that_needs_user_input

为了完整起见,您还可以使用此处文档:

./script_that_needs_user_input << EOF
y
y
y
EOF

或者如果您的 shell 支持此处字符串:

./script <<< "y
y
y
"

或者您可以创建一个每行一个输入的文件:

./script < inputfile

同样,此答案的所有功劳都归于致 askubuntu.com 上的答案的作者 lesmana。

You might not have the ability to install Expect on the target server.
This is often the case when one writes, say, a Jenkins job.

If so, I would consider something like the answer to the following on askubuntu.com:

https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line

printf 'y\nyes\nno\nmaybe\n' | ./script_that_needs_user_input

Note that in some rare cases the command does not require the user to press enter after the character. in that case leave the newlines out:

printf 'yyy' | ./script_that_needs_user_input

For sake of completeness you can also use a here document:

./script_that_needs_user_input << EOF
y
y
y
EOF

Or if your shell supports it a here string:

./script <<< "y
y
y
"

Or you can create a file with one input per line:

./script < inputfile

Again, all credit for this answer goes to the author of the answer on askubuntu.com, lesmana.

丶视觉 2024-12-15 02:31:19

您只需将 -y 与安装命令放在一起即可。

例如: yum install-y

You just need to put -y with the install command.

For example: yum install <package_to_install> -y

我不会写诗 2024-12-15 02:31:19

尽管这可能比您想要的更复杂/更重,但一种非常灵活的方法是使用类似 期望(或另一种编程语言的派生语言之一)。

Expect 是一种专门为控制基于文本的应用程序而设计的语言,这正是您想要做的。如果您最终需要做一些更复杂的事情(比如用逻辑来实际决定下一步要做什么/回答什么),那么 Expect 就是正确的选择。

Although this may be more complicated/heavier-weight than you want, one very flexible way to do it is using something like Expect (or one of the derivatives in another programming language).

Expect is a language designed specifically to control text-based applications, which is exactly what you are looking to do. If you end up needing to do something more complicated (like with logic to actually decide what to do/answer next), Expect is the way to go.

荒路情人 2024-12-15 02:31:19

如果您只想接受默认值,您可以使用:

\n | ./shell_being_run

If you want to just accept defaults you can use:

\n | ./shell_being_run
不忘初心 2024-12-15 02:31:19

我们还可以仅使用 echo 和 shell 替换来对序列进行计时。

以下示例调度 y(是),等待 3 秒,回答 n(否),等待 3 秒,然后按 ENTER:

(echo -n "y"; sleep 3; echo -n "n" ; sleep 3; echo -n "\n") | program

We could also time the sequence, with only echo and shell substitution.

The following example dispatch y (yes), wait 3 seconds, answer n (no), wait 3 seconds, and press ENTER:

(echo -n "y"; sleep 3; echo -n "n" ; sleep 3; echo -n "\n") | program
风向决定发型 2024-12-15 02:31:19

只需执行以下操作:

echo y |

例如:echo y | apt-get 更新

Just do this:

echo y | <your_command>

For example: echo y | apt-get update

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