简体   繁体   中英

Run linux program from shell script with multi-line input

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?

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

The read command reads a single line, terminated by a newline. You can include newlines in your echo:

echo "t\\n3\\n12\\ne" | prog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM