Linux 如何为安装程序编写“是”响应脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7642674/
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
How do I script a "yes" response for installing programs?
提问by user974887
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”,我现在才知道该怎么做。
采纳答案by P.T.
The 'yes' commandwill echo 'y' (or whatever you ask it to) indefinitely. Use it as:
在“是”命令将回声“Y”(或任何你问它)无限期。将其用作:
yes | command-that-asks-for-input
or, if a capital 'Y' is required:
或者,如果需要大写“Y”:
yes Y | command-that-asks-for-input
回答by Adam Batkin
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. 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 是一种专为控制基于文本的应用程序而设计的语言,这正是您想要做的。如果你最终需要做一些更复杂的事情(比如用逻辑来实际决定下一步做什么/回答什么),Expect 是你要走的路。
回答by Dennis
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
适用apt-get
于 Ubuntu。
回答by Nathan Basanese
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.
您可能无法在目标服务器上安装 Expect。例如,当一个人编写一份 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. in that case leave the newlines out:
请注意,在极少数情况下,该命令不需要用户在字符后按 Enter。在这种情况下,请忽略换行符:
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。
回答by warhansen
If you want to just accept defaults you can use:
如果您只想接受默认值,您可以使用:
\n | ./shell_being_run
回答by Rohan Seth
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