Reputation: 488
I'm looking to a way to automate a script execution, which has 5 prompts: 4xYES
and 1*NO
.
The prompts are constantly located on the same place:
yes | yes | yes | no | yes
Something like in the example below (based on 3 prompts):
#!/bin/bash
read -r -p "Ready to proceed? [yes/no]: " input
if [[ "$input" = "yes" ]]
then
read -r -p "Or maybe abort? [yes/no]: " answer
if [[ "$answer" = "no" ]]
then
read -r -p "So... proceed? [yes/no]: " response
if [[ "$response" = "yes" ]]
then echo "NICE JOB!!!"
fi
fi
fi
Obviously, passing a single "yes" or "no" via the pipe to this script will no work, as the expected responses are different. Not aware whether there is a way to pass multiple params via the pipe.
I thought to create an array PROMPT=(yes no yes)
, and pass $i
via the pipe to the script, but this will not work as well, and i do not need cyclic script execution for each $i
.
Is there any way to pass different answers to the prompt, if their places are constant?
Upvotes: 1
Views: 2123
Reputation: 4698
You could use printf
and a pipe
printf '%s\n' yes yes yes no yes | ./script.sh
or input redirection and a process substitution
./script.sh < <(printf '%s\n' yes yes yes no yes)
or a here-string
./script.sh <<<$'yes\nyes\nyes\nno\nyes'
or a here-document
./script.sh <<'EOF'
yes
yes
yes
no
yes
EOF
Upvotes: 6
Reputation: 519
By 'simulating' a 'enter' with a new line
Something like:
echo "yes\nno\nyes" | bash myscript.sh
Pass your bash script
Upvotes: 0