Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch from file contents to STDIN in piped command? (Linux Shell)

I have a program (that I did not write) which is not designed to read in commands from a file. Entering commands on STDIN is pretty tedious, so I'd like to be able to automate it by writing the commands in a file for re-use. Trouble is, if the program hits EOF, it loops infinitely trying to read in the next command dropping an endless torrent of menu options on the screen.

What I'd like to be able to do is cat a file containing the commands into the program via a pipe, then use some sort of shell magic to have it switch from the file to STDIN when it hits the file's EOF.

Note: I've already considered using cat with the '-' for STDIN. Unfortunately (I didn't know this before), piped commands wait for the first program's output to terminate before starting the second program -- they do not run in parallel. If there's some way to get the programs to run in parallel with that kind of piping action, that would work!

Any thoughts? Thanks for any assistance!

EDIT:

I should note that my goal is not only to prevent the system from hitting the end of the commands file. I would like to be able to continue typing in commands from the keyboard when the file hits EOF.

like image 236
zslayton Avatar asked Oct 13 '09 15:10

zslayton


4 Answers

I would do something like

(cat your_file_with_commands; cat) | sh your_script

That way, when the file with commands is done, the second cat will feed your script with whatever you type on stdin afterwards.

like image 77
Idelic Avatar answered Nov 13 '22 03:11

Idelic


Same as Idelic answer with more simple syntax ;)

cat your_file_with_commands - | sh your_script
like image 38
Guillaume Avatar answered Nov 13 '22 01:11

Guillaume


I would think expect would work for this.

like image 33
Dennis Williamson Avatar answered Nov 13 '22 03:11

Dennis Williamson


Have you tried using something like tail -f commandfile | command I think that should pipe the lines of the file to command without closing the file descriptor afterwards. Use -n to specify the number of lines to be piped if tail -f doesn't catch all of them.

like image 45
Michiel Buddingh Avatar answered Nov 13 '22 03:11

Michiel Buddingh