I'm writing a Linux Shell Script to automate a few things I'm doing on Ubuntu 11.04.
我正在编写一个Linux Shell脚本来自动完成我在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.
基本上,我正在编写一个shell脚本来安装NGINX,MySQL和PHP,然后配置所有内容。我知道如何通过命令行完成所有事情。
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.
但是,我不知道我将如何处理进程要求用户输入的部分。例如,我使用apt-get安装的某些内容会要求您进行确认,即(Y)es或(N)o。
How exactly would I handle auto-confirmation in the shell script i.e. to automatically confirm Yes or No when asked?
我究竟如何处理shell脚本中的自动确认,即在被问到时自动确认是或否?
4 个解决方案
#1
15
yes | ./script
will answer y
for everything.
是的| ./script会回答所有事情。
Otherwise, write a script that prints the answers you want, eg:
否则,编写一个打印所需答案的脚本,例如:
echo N
echo Y
echo Y
#2
4
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 :
通常,您可以调用此类交互式程序,并选择自动对所有问题回答“是”。例如,您可以使用-y调用apt-get。从手册页:
-y, --yes, --assume-yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively.
-y, - 是, - assume-yes自动提示是;假设“是”作为所有提示的答案并以非交互方式运行。
#3
2
For 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
对于Apt,正确的答案是使用正确的参数“预置”您的debconf数据库。如果Debconf从其数据库中找到答案,它将不会询问。另见http://www.debian-administration.org/articles/394
#1
15
yes | ./script
will answer y
for everything.
是的| ./script会回答所有事情。
Otherwise, write a script that prints the answers you want, eg:
否则,编写一个打印所需答案的脚本,例如:
echo N
echo Y
echo Y
#2
4
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 :
通常,您可以调用此类交互式程序,并选择自动对所有问题回答“是”。例如,您可以使用-y调用apt-get。从手册页:
-y, --yes, --assume-yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively.
-y, - 是, - assume-yes自动提示是;假设“是”作为所有提示的答案并以非交互方式运行。
#3
2
For 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
对于Apt,正确的答案是使用正确的参数“预置”您的debconf数据库。如果Debconf从其数据库中找到答案,它将不会询问。另见http://www.debian-administration.org/articles/394