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?

the paul
  • 8,972
  • 1
  • 36
  • 53
ObiHill
  • 11,448
  • 20
  • 86
  • 135
  • Duplicate? http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script (hope it helps!) – heltonbiker Sep 14 '11 at 03: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. – ObiHill Sep 14 '11 at 11:19
  • Your're right, I read it wrong, sorry. – heltonbiker Sep 14 '11 at 12:40

4 Answers4

20

yes | ./script will answer y for everything.

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

 echo N
 echo Y
 echo Y
wormsparty
  • 2,481
  • 19
  • 31
  • 1
    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? – ObiHill Sep 14 '11 at 13:29
  • 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` – wormsparty Sep 14 '11 at 13:34
  • 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. – ObiHill Sep 14 '11 at 13:51
  • What would you echo if the prompt asks you for a non-character keystroke e.g. "press enter to continue"? – Sledge Jan 27 '20 at 15:46
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.

touffy
  • 147
  • 2
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

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The link is now quite out of date. More recent documentation exists at e.g. https://wiki.debian.org/DebianInstaller/Preseed and in [Appendix B of the Debian Installer manual](https://www.debian.org/releases/stable/amd64/apb) but these are not particularly beginner-friendly. https://thornelabs.net/posts/debian-ubuntu-preseed-documentation-and-working-examples.html looks more approachable, but also somewhat prone to future link rot. – tripleee Jul 18 '21 at 15:36
1

Try Expect it might be what you are looking for.

LordDoskias
  • 3,121
  • 3
  • 30
  • 44
  • 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?! – ObiHill Sep 14 '11 at 13:38