Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I respond to prompts in a Linux Bash script automatically? [duplicate]

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).

like image 334
Ron Avatar asked Nov 24 '16 17:11

Ron


People also ask

How do I automatically answer yes to a prompt in Linux?

The y characters from the yes command will respond "yes" to each prompt automatically. Here, the yes command outputs "n" in a constant stream to the rm -i command, answering "no" to all the same questions.

What does $@ do in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you repeat a command line in Linux?

First, the easiest way to repeat a command is simply by typing !!. If you were logged into a Linux server and waiting for a coworker to log in, for example, you might want to repeat the who command shown below until you see your coworker's username. Typing !! after the initial who command will do this for you.

How do I run the same script multiple times in Linux?

Repeating a Command Using 'for' Loop You can use a 'for' loop to print a certain command multiple times. There are different variations of the 'for' loop, and we will explore all of them with the help of different bash scripts.


2 Answers

Pipe to Standard Input

Some scripts can take replies from standard input. One of the many ways to do this would be:

$ printf "%s\n" yes yes no | ./foo.sh 
yes yes no

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.

#!/usr/bin/env bash
for question in Foo Bar Baz; do
    read -p "${question}? "
    replies=("${replies[@]}" "$REPLY")
done
echo "${replies[@]}"

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!

#!/usr/bin/env expect
spawn -noecho /tmp/foo.sh
expect "Foo? " { send -- "1\r" }
expect "Bar? " { send -- "2\r" }
expect "Baz? " { send -- "3\r" }
interact

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.

$ /tmp/foo.exp 
Foo? 1
Bar? 2
Baz? 3
1 2 3
like image 185
Todd A. Jacobs Avatar answered Oct 23 '22 19:10

Todd A. Jacobs


Try this:

echo -e "yes\nyes\nno" | /path/to/your/script

From help echo:

-e: enable interpretation of the following backslash escapes

like image 20
Cyrus Avatar answered Oct 23 '22 20:10

Cyrus