237

I'm running a script that it requests entering 'y' on each operation, I am looking for a solution like $ ./script < echo 'yyyyyyyyyyyyyy' to pass all my inputs in one time.

NewMrd
  • 2,687
  • 4
  • 15
  • 9

8 Answers8

393

There is a command created specifically for that case: yes

$ yes | ./script

What this does is connect the output of yes to the input of ./script. So when ./script asks for user input it will instead get the output of yes. The output of yes is an endless stream of y followed by enter. So basically as if the user is entering y for every question of ./script.

If you want to say no (n) instead of yes (y) you can do it like this:

$ yes n | ./script

Note that some tools have an option to always asume yes as answer. See here for example: Bypass the yes/no prompt in 'apt-get upgrade'


Other methods to enter input:

If you know exactly how many y your script is expecting you can do it like this:

$ printf 'y\ny\ny\n' | ./script

The newlines (\n) are the enter keys.

Using printf instead of yes you have more fine grained control of input:

$ printf 'yes\nno\nmaybe\n' | ./script

Note that in some rare cases the command does not require the user to press enter after the character. in that case leave the newlines out:

$ printf 'yyy' | ./script

For sake of completeness you can also use a here document:

$ ./script << EOF
y
y
y
EOF

Or if your shell supports it a here string:

$ ./script <<< "y
y
y
"

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

$ ./script < inputfile

If the command is sufficiently complex and the methods above no longer suffice then you can use expect.

Here is an example of a super simple expect script:

spawn ./script
expect "are you sure?"
send "yes\r"
expect "are you really sure?"
send "YES!\r"
expect eof

Technical nitpick:

The hypothetical command invocation you gave in your question does not work:

$ ./script < echo 'yyyyyyyyyyyyyy'
bash: echo: No such file or directory

This is because the shell grammar allows a redirect operator anywhere in the command line. As far as the shell is concerned your hypothetical command line is the same as this line:

$ ./script 'yyyyyyyyyyyyyy' < echo
bash: echo: No such file or directory

That means ./script will be called with the argument 'yyyyyyyyyyyyyy' and the stdin will get input from a file named echo. And bash complains since the file does not exists.

Lesmana
  • 18,386
  • 2
    I get a cannot enable tty mode on non tty input. Would you know a workaround for that? – bmpasini Apr 22 '16 at 19:33
  • When I try the printf trick with a run file that I need to automate the installation process of, all that happens is I get an error message saying Warning: Tried to connect to session manager, None of the authentication protocols specified are supported, and the script opens in a new terminal and asks me to enter my input manually as usual. This is happening on Debian by the way. Any suggestions? – DaveTheMinion Jul 11 '17 at 18:45
  • need more details. too big for comments. please ask a new question with all the details. you can put a link here. – Lesmana Jul 11 '17 at 19:55
  • nothing works with 'vagrant destroy' on centos – Drew Sep 04 '17 at 18:06
  • Wicked easy! Works on the Google Cloud Beta SQL data imports! Much appreciated! – G_Style Mar 30 '18 at 18:12
  • I just learned that actually /bin/yes can send any argument given, like yes no or maybe not only single characters. – Björn Jan 08 '19 at 08:57
  • Does the command $ printf 'yyy' | ./script send 3 yes three times, or is it a syntax to send one yes? I assume it is the first option since it is in context of sending the yes command multiple times, but I have not found a way to test it. – a.t. Feb 08 '19 at 10:56
  • @lesmana Is there any way to edit the shell file to have the script itself enter yes or no for us? – Shayan Aug 29 '19 at 15:40
  • Hi @Lesmana, melos command gives me StdinException: Error getting terminal echo mode, OS Error: Inappropriate ioctl for device, errno = 25 when I pass output of yes to it. Is it possible to fix this?

    Command: $ yes 33 | melos run analyze

    – vahotm Apr 04 '22 at 12:10
  • to big for comment. please post is as a new question. feel free to drop link to new question here. – Lesmana Apr 04 '22 at 13:06
  • printf y\n without quotation marks will print yn whereas single and double quoation marks as in printf 'y\n'work and print a new line. – Timo Apr 27 '22 at 18:56
  • expect never worked for me. For what is worth: $ ./script < inputfile worked wonders for me automating my openvpn3 login ;) Thanx – DimiDak May 11 '23 at 15:57
14

Use the command yes:

yes | script

Excerpt from the man page:

NAME
       yes - output a string repeatedly until killed

SYNOPSIS
       yes [STRING]...
       yes OPTION

DESCRIPTION
       Repeatedly output a line with all specified STRING(s), or 'y'.
Peter W. Osel
  • 141
  • 1
  • 2
10

Some things (apt-get for example) accept special flags to run in silent mode (and accept defaults). In apt-get's case, you just pass it a -y flag. This does completely depend on your script though.

If you need more complicated things, you can wrap your script in an expect script. expect allows you to read output and send input so you can do pretty complicated things that other scripting wouldn't allow. Here's one of the examples from its Wikipedia page:

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
Oli
  • 293,335
7

In the shell script you can also use the following trick of spawn, expect and send

spawn script.sh
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes"

However in the above scenerio you will have to give the phrase you are expecting to get while you execute the script for more examples go to the following link

Expect within Bash

Tarun
  • 4,245
  • 13
  • 50
  • 74
3

You can supply user input to your script with cat, from a text file, piped to your script with bash like this:

cat input.txt | bash your_script.sh

Just put your desired user input into your input.txt file, whatever answers you want - y, n, digits, strings, etc.

SudoSURoot
  • 2,829
  • 2
  • 14
  • 16
WebComer
  • 225
2

Okay, this may not be a very elegant solution but if you write your options in a separate file and then pass it as an input to the script, it would work as well. So if you create a new file with all your options (call this file as 'options.in'), then you can easily run your script with ./script.sh < options.in and edit/create different options files as suitable.

Sidhha
  • 121
  • 3
2

I was writing a bash script with Dialog and needed this to happen automatically also. I did this and it worked like a charm.

# -Wy force signaturewipe (if exists)
echo "y" | sudo lvcreate -W y -n $lvName -L "$lvSize"G /dev/"$svg" >> $nfsUtilLog
-1

You can write the expect script as follow:

$ vi login.exp
#!/usr/bin/expect -f
spawn ssh username@machine.IP
expect "*assword: "
send -- "PASSWORD\r"
interact

And run it as:

$ expect login.exp