How to make the script answer "yes" to install programs?

I work with instances of Amazon Linux, and I have several scripts to populate the data and install all the programs that I work with, but several programs ask:

Do you want to continue [Y/n]? 

and pause the installation. I want in all cases to automatically answer "Y", now I know exactly how to do this.

+93
linux scripting bash
04 Oct 2018-11-11T00:
source share
6 answers

The command 'yes' will echo 'y' (or whatever you ask) for an indefinite period. Use it as:

 yes | command-that-asks-for-input 

or, if capital "Y" is required:

 yes Y | command-that-asks-for-input 
+178
04 Oct 2018-11-11T00:
source share

echo y | command echo y | command should work.

In addition, some installers have an auto-yes flag. He is -y for apt-get on Ubuntu.

+59
04 Oct '11 at 2:25
source share

You may not be able to install Expect on the target server. This is often the case when someone writes, say, Jenkins' work.

If so, I would like to consider something like the answer to the following question on askubuntu.com:

https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line

 printf 'y\nyes\nno\nmaybe\n' | ./script_that_needs_user_input 

Note that in some rare cases, the command does not require the user to press enter after the character. in this case, leave a newline:

 printf 'yyy' | ./script_that_needs_user_input 

For completeness, you can also use the document here:

 ./script_that_needs_user_input << EOF y y y EOF 

Or, if your shell supports the line here:

 ./script <<< "y y y " 

Or you can create a file with one input per line:

 ./script < inputfile 

Again, all the credit for this answer belongs to the author of the answer on askubuntu.com , Lesmana.

+14
Dec 09 '15 at 10:44
source share

Although it may be harder / harder than you want, one very flexible way to do this is to use something like Expect (or one of the derivatives in another programming language).

Expect is a language designed specifically for managing text-based applications, and this is exactly what you want to do. If you eventually need to do something more complex (for example, using logic to actually decide what to do / answer next), Expect is the way to go.

+5
04 Oct 2018-11-11T00:
source share

If you want to just accept the defaults, you can use:

 \n | ./shell_being_run 
+3
Jun 13 '18 at 8:08
source share

You just need to put -y with the install command.

For example: yum install <package_to_install> -y

+2
Aug 31 '19 at 20:24
source share



All Articles