Bash script - Autocomplete response
I have a bash script that has a few questions, is it possible to auto-fill in the answers?
./script.sh install
answers ok y 2 1 n n
How do I do this in bash?
edit: is it possible to pass only the first answer?
echo "y" | install
and enable the user to answer the following questions?
source to share
I would pass the document here to stdin:
./script.sh install <<EOF
y
2
1
n
n
EOF
If you want it on one line, you can also use echo
:
echo -e "y\n2\n1\nn\nn" | ./script.sh install
However, I prefer the solution here from the doc as it is IMHO more readable.
source to share
Another method is to use a string here (which allows you to exclude a one-liner, but not a subshell):
./script.sh install <<<$(printf "y\n2\n1\nn\nn\n")
You can also rely on the printf trick to print all elements using a single format specifier and use process substitution (or use with the syntax syntax here above):
./script.sh install < <(printf "%c\n" y 2 1 n n)
source to share