在 Linux shell 脚本中处理输入确认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7410771/
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
Handling input confirmations in Linux shell scripting
提问by Obinwanne Hill
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 脚本中的自动确认,即在询问时自动确认是或否?
采纳答案by wormsparty
yes | ./script
will answer y
for everything.
yes | ./script
会回答 y
一切。
Otherwise, write a script that prints the answers you want, eg:
否则,编写一个脚本来打印您想要的答案,例如:
echo N
echo Y
echo Y
回答by tripleee
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
回答by touffy
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-get
使用-y
. 从手册页:
-y, --yes, --assume-yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively.
-y, --yes, --assume-yes 提示自动是;假设“是”作为对所有提示的回答并以非交互方式运行。