简体   繁体   English

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

[英]How do I script a "yes" response for installing programs?

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:我使用 Amazon Linux 实例,我有几个脚本来填充数据并安装我使用的所有程序,但有几个程序会询问:

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.我想在所有情况下都自动回答“Y”,我现在才知道该怎么做。

The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. “是”命令将无限期地回显“y”(或您要求的任何内容)。 Use it as:将其用作:

yes | command-that-asks-for-input

or, if a capital 'Y' is required:或者,如果需要大写“Y”:

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

If you want to pass 'N' you can still use yes :如果你想通过 'N' 你仍然可以使用yes

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

echo y | command echo y | command should work. echo y | command应该可以工作。

Also, some installers have an "auto-yes" flag.此外,一些安装程序有一个“自动是”标志。 It's -y for apt-get on Ubuntu.它是-y用于 Ubuntu 上的apt-get

You might not have the ability to install Expect on the target server.您可能无法在目标服务器上安装 Expect。 This is often the case when one writes, say, a Jenkins job.例如,当一个人编写一份 Jenkins 工作时,通常就是这种情况。

If so, I would consider something like the answer to the following on askubuntu.com:如果是这样,我会考虑在 askubuntu.com 上对以下问题的回答:

https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line 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.请注意,在极少数情况下,该命令不需要用户在字符后按 Enter。 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:或者,如果您的 shell 支持此处的字符串:

./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.同样,此答案的所有功劳都归功于askubuntu.com的答案作者 lesmana。

You just need to put -y with the install command.您只需要在安装命令中加上-y

For example: yum install <package_to_install> -y例如: yum install <package_to_install> -y

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 (或另一种编程语言中的派生词之一)之类的方法。

Expect is a language designed specifically to control text-based applications, which is exactly what you are looking to do. Expect 是一种专为控制基于文本的应用程序而设计的语言,这正是您想要做的。 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.如果你最终需要做一些更复杂的事情(比如用逻辑来实际决定下一步做什么/回答什么),Expect 是你要走的路。

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

\n | ./shell_being_run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM