Handling input confirmations in Linux shell scripting

青春壹個敷衍的年華 提交于 2019-12-03 12:48:58

问题


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?


回答1:


yes | ./script will answer y for everything.

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

 echo N
 echo Y
 echo Y



回答2:


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.




回答3:


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




回答4:


Try Expect it might be what you are looking for.



来源:https://stackoverflow.com/questions/7410771/handling-input-confirmations-in-linux-shell-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!