在 Linux shell 脚本中处理输入确认
我正在编写一个 Linux Shell 脚本来自动化我在 Ubuntu 11.04 上做的一些事情。
基本上,我正在编写一个 shell 脚本来安装 NGINX、MySQL 和 PHP,然后配置所有内容。我知道如何通过命令行完成所有事情。
但是,我不知道如何处理流程要求用户输入的部分。例如,我使用 apt-get 安装的某些东西会要求您确认,即 (Y)es 或 (N)o。
我究竟如何处理 shell 脚本中的自动确认,即在询问时自动确认“是”或“否”?
I'm writing a Linux Shell Script to automate a few things I'm doing on Ubuntu 11.04.
Basically, I'm writing a shell script to install NGINX, MySQL, and PHP, and then configure everything. I know how to do everything via the command-line.
However, I don't know how I'm going to handle the parts where the process asks for user input. For example, certain things I install with apt-get ask you for a confirmation i.e. (Y)es or (N)o.
How exactly would I handle auto-confirmation in the shell script i.e. to automatically confirm Yes or No when asked?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的 | ./script
将为所有问题回答y
。否则,编写一个脚本来打印您想要的答案,例如:
yes | ./script
will answery
for everything.Otherwise, write a script that prints the answers you want, eg:
通常,您可以调用此类交互式程序,并选择自动对所有问题回答“是”。例如,您可以使用
-y
调用apt-get
。从手册页:Usually you can call such interactive programs with an option to automatically answer yes to all questions. For instance, you can call
apt-get
with-y
. From the man page :对于 Apt,正确的答案是使用正确的参数“预置”您的
debconf
数据库。如果 Debconf 从其数据库中找到答案,它就不会询问。另请参阅http://www.debian-administration.org/articles/394For Apt, the correct answer is to "preseed" your
debconf
database with the correct parameters. If Debconf finds the answer from its database, it won't ask. See also http://www.debian-administration.org/articles/394尝试期望它可能就是您正在寻找的东西。
Try Expect it might be what you are looking for.