Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run linux program from shell script with multi-line input

Tags:

linux

shell

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?

like image 407
hp7289 Avatar asked Nov 20 '13 16:11

hp7289


People also ask

How do you execute a multi line command in shell?

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.

How do I input multiple inputs in bash?

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.


2 Answers

Prime use case for a here-document:

prog <<EOF
t
3
12
e
EOF
like image 181
twalberg Avatar answered Nov 07 '22 22:11

twalberg


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
like image 39
liuyu Avatar answered Nov 07 '22 22:11

liuyu