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
Does Malloc Lazily Create the Backing Pages For an Allocation on Linux (And Other Platforms)
How to Split CSV Files as Per Number of Rows Specified
How to Start Solr Automatically
Use Bluez Stack as a Peripheral (Advertiser)
Linux and I/O Completion Ports
Encryption/Decryption Doesn't Work Well Between Two Different Openssl Versions
How to Install Gcc 4.9.2 on Rhel 7.4
How to Recursively Find All Files in Current and Subfolders Based on Wildcard Matching
Pseudo-Terminal Will Not Be Allocated Because Stdin Is Not a Terminal
How to Print Third Column to Last Column
How to Create a Crontab Through a Script
How to Check If a File Is Empty in Bash
How Do Linux Binary Installers (.Bin, .Sh) Work
Linux/Unix Command to Determine If Process Is Running
Adding a New Entry to the Path Variable in Zsh
How to Join Multiple Lines of File Names into One With Custom Delimiter