I want to run a specific program from a script which normally aks the user for some input (several times).
For example, when I start the program in the shell and my input would be:
t [ENTER]
3 [ENTER]
12 [ENTER]
e [ENTER]
where one has to wait after every line that the program wants the next input.
I guess there is a solution like
echo t | prog
echo 3 | prog
echo 12 | prog
echo e | prog
but after the first line the program runs with no input because of an empty buffer. How can I fix that?
Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.
The multiple inputs can be taken at a time by using the read command with multiple variable names. In the following example, four inputs will be taken in four variables by using the read command. Output: The following output will appear after executing the above script.
Prime use case for a here-document:
prog <<EOF
t
3
12
e
EOF
Guess it depends on what kind of shell you are using. With bash
you can echo
multiple lines like,
$ echo "t
> 3
> 12
> e" | prog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With