This question already has an answer here:
I am running a script (I can't edit it), and there are three yes/no questions. How can I automatically respond to these questions? I need to answer yes, yes, no (in that order).
This question already has an answer here:
I am running a script (I can't edit it), and there are three yes/no questions. How can I automatically respond to these questions? I need to answer yes, yes, no (in that order).
Try this:
Pipe to Standard Input
Some scripts can take replies from standard input. One of the many ways to do this would be:
This is simple and easy to read, but relies on how your script internals handle standard input, and if you can't edit the target script that can sometimes be a problem.
Use Expect for Interactive Prompts
While you can sometimes get away with using standard input, interactive prompts are generally better handled by tools like Expect. For example, given a script foo.sh, you can write foo.exp to automate it.
Note: You can also use autoexpect to create a a script from an interactive session, which you can then edit if necessary. I'd highly recommend this for people new to Expect.
Bash Script: foo.sh
This is the script you might want to automate.
Expect Script: foo.exp
Here is a simplistic Expect script to automate the Bash script above. Expect loops, branching, and regular expressions can provide much more flexibility than this oversimplified example shows, but it does show how easy a minimal Expect script can be!
Sample Interactive Session
This is what your interactive session will look like when you run the Expect script. It will spawn your Bash script, and respond as instructed to each different prompt.