Have Bash Script Answer Interactive Prompts

Have bash script answer interactive prompts

This is not "auto-completion", this is automation. One common tool for these things is called Expect.

You might also get away with just piping input from yes.

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

Try this:

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

From help echo:

-e: enable interpretation of the following backslash escapes

How to have bash script answer interactive prompts (no y/n only)?

Consider these two test scripts:

tmp()
{
var=fred
tmp2 << EOD
$var
EOD
}

tmp2 ()
{
read var2
echo $var2
}

If you paste those into a shell, then run tmp, you'll get this:

> tmp
fred

The "heredoc" syntax lets you include responses to prompts in your script after the program. So what you want to do is

php ./console phpci:install <<EOD
$HOST
$DATABASE
$USERNAME
$PASSWORD
[ ... etc ... ]
EOD

That should do it for you. Note that you can include hard-coded values (instead of the variables) as well.

You can read more about heredoc at Wikipedia if you like.

Hope this helps!

Detect and Enter Input on a prompt in shell script

Use the yes command to answer interactive prompts,

yes Y | ./script.sh

The above syntax constantly puts the string Y to all your prompts. You can pass the string as you need after yes.

You can also use expect tool meant for this, but you need to know the exact prompt message for capturing and responding to it accordingly. If your prompt is simple and just need a simple input to pass yes would be the right tool.

Also you can use bash built-in printf, but you need to add the responses manually depending upon the number of prompts you have to respond to, e.g.

printf 'Y\nY\nY\n' | ./script.sh

to send response as Y for three prompts. As again, to avoid doing this manually, prefer using the yes command.



Related Topics



Leave a reply



Submit