在 Linux shell 脚本中处理输入确认

发布于 2024-12-04 08:29:17 字数 259 浏览 9 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红颜悴 2024-12-11 08:29:17

是的 | ./script 将为所有问题回答 y

否则,编写一个脚本来打印您想要的答案,例如:

 echo N
 echo Y
 echo Y

yes | ./script will answer y for everything.

Otherwise, write a script that prints the answers you want, eg:

 echo N
 echo Y
 echo Y
初见终念 2024-12-11 08:29:17

通常,您可以调用此类交互式程序,并选择自动对所有问题回答“是”。例如,您可以使用 -y 调用 apt-get。从手册页:

-y,--是,--假设-是
自动提示“是”;假设“是”作为所有提示的答案并运行
非交互式。

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, --yes, --assume-yes
Automatic yes to prompts; assume "yes" as answer to all prompts and run
non-interactively.

伏妖词 2024-12-11 08:29:17

对于 Apt,正确的答案是使用正确的参数“预置”您的 debconf 数据库。如果 Debconf 从其数据库中找到答案,它就不会询问。另请参阅http://www.debian-administration.org/articles/394

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

动次打次papapa 2024-12-11 08:29:17

尝试期望它可能就是您正在寻找的东西。

Try Expect it might be what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文