Switch from file contents to STDIN in piped comman

2019-03-20 16:19发布

问题:

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.

回答1:

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.



回答2:

I would think expect would work for this.



回答3:

Same as Idelic answer with more simple syntax ;)

cat your_file_with_commands - | sh your_script


回答4:

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.