15

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?

3
  • Duplicate? stackoverflow.com/questions/226703/… (hope it helps!) Commented Sep 14, 2011 at 3:06
  • @heltonbiker: I'm not trying to get input from the user of the shell script, I'm trying to force/make input wherever my script would usually have prompted me to provide a Yes or No response. Commented Sep 14, 2011 at 11:19
  • Your're right, I read it wrong, sorry. Commented Sep 14, 2011 at 12:40

4 Answers 4

21

yes | ./script will answer y for everything.

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

 echo N
 echo Y
 echo Y
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this option. I'm not sure how exactly I would structure the script to do this though, do you have a code sample?
If you don't want to answer always 'yes', I don't really get your question, since it's completely case-dependant. If you want to answer "yes yes no" to a script, simply: printf "y\ny\nn\n" | ./your_script or write a simple script with seperate lines to print each answer, and then: ./answer_script | ./the_script
Thanks a lot. I guess I have multiple options now just in case I want to answer differently to different prompts. This appears to be the most elegant way to do what I want to do being that I only need to call it once with the script.
What would you echo if the prompt asks you for a non-character keystroke e.g. "press enter to continue"?
3

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.

Comments

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

1 Comment

The link is now quite out of date. More recent documentation exists at e.g. wiki.debian.org/DebianInstaller/Preseed and in Appendix B of the Debian Installer manual but these are not particularly beginner-friendly. thornelabs.net/posts/… looks more approachable, but also somewhat prone to future link rot.
1

Try Expect it might be what you are looking for.

1 Comment

Thanks a lot for this, it looks very interesting. Would I have to know exactly what the prompt is going to be i.e. if the command I run in my shell script is going to do something like "Enter your date of birth:", would I have to write Expect "Enter your date of birth:" exactly as the prompt is going to ask for it?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.