如何编写“是”的脚本安装程序的响应?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
'yes' 命令 将回显 'y' (或您要求的任何内容)无限期地。使用它:
或者,如果需要大写“Y”:
如果您想传递“N”,您仍然可以使用
yes
:The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. Use it as:
or, if a capital 'Y' is required:
If you want to pass 'N' you can still use
yes
:回声 y |命令应该可以工作。
此外,一些安装程序有一个“自动是”标志。 Ubuntu 上的
apt-get
是-y
。echo y | command
should work.Also, some installers have an "auto-yes" flag. It's
-y
forapt-get
on Ubuntu.您可能无法在目标服务器上安装 Expect。
当人们编写 Jenkins 作业时,通常会出现这种情况。
如果是这样,我会考虑在askubuntu.com上回答以下问题:
https: //askubuntu.com/questions/338857/automatically-enter-input-in-command-line
请注意,在极少数情况下,该命令不需要用户在字符后按 Enter 键。在这种情况下,请保留换行符:
为了完整起见,您还可以使用此处文档:
或者如果您的 shell 支持此处字符串:
或者您可以创建一个每行一个输入的文件:
同样,此答案的所有功劳都归于致 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
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:
For sake of completeness you can also use a here document:
Or if your shell supports it a here string:
Or you can create a file with one input per line:
Again, all credit for this answer goes to the author of the answer on askubuntu.com, lesmana.
您只需将
-y
与安装命令放在一起即可。例如: yum install-y
You just need to put
-y
with the install command.For example:
yum install <package_to_install> -y
尽管这可能比您想要的更复杂/更重,但一种非常灵活的方法是使用类似 期望(或另一种编程语言的派生语言之一)。
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.
如果您只想接受默认值,您可以使用:
If you want to just accept defaults you can use:
我们还可以仅使用 echo 和 shell 替换来对序列进行计时。
以下示例调度 y(是),等待 3 秒,回答 n(否),等待 3 秒,然后按 ENTER:
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 y |
例如:
echo y | apt-get 更新
Just do this:
echo y | <your_command>
For example:
echo y | apt-get update