Reputation: 491
I need to pass yes or no input to a bash script. It works for single single input using yes
, however if the bash script has multiple input (more than one) how can I pass it ? Below is the example for a single yes input:
yes | ./script
I can't send as below if the script needs different input, for instance:
yes | no | ./script
Upvotes: 1
Views: 6654
Reputation: 491
We can also use below command also
echo -e "yes\nno" | ./script.sh
Upvotes: 2
Reputation: 15273
You can just send the lines you want -
printf "yes\nno\n" | ./script.sh
or if you need a certain number of each -
{ yes yes |head -50; yes no | head -50; } | ./script.sh
Or if you need them toggled -
yes | awk '{ if (NR%2) {print "yes"} else {print "no"} }' | ./script.sh
(only using yes
here as a driver for records to read, lol)
Upvotes: 0
Reputation: 70792
You can pass 1 yes
, then 1 no
, then again 1 yes
and 1 no
and so on by using:
yes $'yes\nno' | ./script.sh
Using bash, you could revert pipe by using this syntaxe:
./script < <(yes $'yes\nno')
Sample:
head -n 6 < <(yes $'yes\nno')
yes
no
yes
no
yes
no
... or two yes
and one no
:
head -n 6 < <(yes $'yes\nyes\nno')
yes
yes
no
yes
yes
no
Or any other combination...
Upvotes: 4
Reputation: 241858
yes
sends y
(or whatever else you tell it to send) to its standard output. If you want yes
and no
on the standard output, you can use echo
:
{ echo yes
echo no
} | ./script
I used a block to pipe both inputs to the script. There are more possible ways, e.g.
printf '%s\n' yes no | ./script.sh
Upvotes: 5