Ask the bash script to respond to interactive prompts

Is it possible for bash script commands to automatically process requests that are normally presented to the user with default actions? I am currently using a bash script to invoke an internal tool that will display prompts to the user (prompt for Y / N) to complete the actions, however the script I am writing should be completely "hands-off", so I need to send Y|N at the invitation to continue the program. Is it possible?

+45
linux bash command-prompt automation prompt
Sep 27 '10 at 14:04
source share
5 answers

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

You can also get away with just entering the pipeline from yes .

+38
Sep 27 '10 at 14:08
source share

Plain

 echo "YYNNYNYYN" | ./your_script 

This will allow you to pass any sequence "Y" or "N" to your script.

+89
Sep 27 '10 at 14:12
source share

I found that the best way to send input is to use cat and a text file to transfer the input you need.

 cat "input.txt" | ./Script.sh 
+9
Jul 22 '15 at 14:23
source share

If you only have Y to send:

 $> yes Y |./your_script 

If you only have N to send:

 $> yes N |./yout_script 
+6
Nov 16 '16 at 10:23
source share

In my situation, I needed to answer some questions without Y or N, but with text or a space. I found a better way to do this in my situation, to create a shellscript file. In my case, I called it autocomplete.sh

I needed to answer some questions for the doctrine scheme exporter, so my file looked like this.

- This is an example only -

 php vendor/bin/mysql-workbench-schema-export mysqlworkbenchfile.mwb ./doctrine << EOF `#Export to Doctrine Annotation Format` 1 `#Would you like to change the setup configuration before exporting` y `#Log to console` y `#Log file` testing.log `#Filename [%entity%.%extension%]` `#Indentation [4]` `#Use tabs [no]` `#Eol delimeter (win, unix) [win]` `#Backup existing file [yes]` `#Add generator info as comment [yes]` `#Skip plural name checking [no]` `#Use logged storage [no]` `#Sort tables and views [yes]` `#Export only table categorized []` `#Enhance many to many detection [yes]` `#Skip many to many tables [yes]` `#Bundle namespace []` `#Entity namespace []` `#Repository namespace []` `#Use automatic repository [yes]` `#Skip column with relation [no]` `#Related var name format [%name%%related%]` `#Nullable attribute (auto, always) [auto]` `#Generated value strategy (auto, identity, sequence, table, none) [auto]` `#Default cascade (persist, remove, detach, merge, all, refresh, ) [no]` `#Use annotation prefix [ORM\]` `#Skip getter and setter [no]` `#Generate entity serialization [yes]` `#Generate extendable entity [no]` y `#Quote identifier strategy (auto, always, none) [auto]` `#Extends class []` `#Property typehint [no]` EOF 

What I like about this strategy is that you can comment on what your answers are and using an EOF empty line is easy (default answer). It turns out that this exporter tool has its own JSON counterpart to answer these questions, but I figured it out after I did it =).

run the script just in the right directory and run 'sh autocomplete.sh' in the terminal.

In short, using <EOL and EOF in conjunction with return lines , you can answer every hint question if necessary. Each new line is a new answer.

My example shows how this can be done with comments, also using the `symbol, so that you remember what each step is.

Note another advantage of this method: you can answer with more than Y or N ... in fact, you can answer with spaces!

Hope this helps someone.

+2
Feb 13 '17 at 1:29 on
source share



All Articles