I have a Linux program that gives the user a prompt, takes a one line input from stdin, performs an action, and then returns to the prompt to repeat (unless the action is "exit"). I would like to automate giving an exact sequence of inputs to the program in a way that displays exactly the same as if I had entered it from the keyboard. In other words, to look like this:
prompt> one
prompt> two
prompt> three
My first thought was that I would create a file input.txt
with these contents:
one
two
three
And then run the command:
./a.out < input.txt
This gives the right program behavior but the display shows this, instead of being identical to the first code block above:
prompt> prompt> prompt>
Is there any way I can get the input from a file that will display the inputs (at the correct points) so the display looks like the first code block above?
Important Notes:
I can modify the input file however I want, but I cannot modify the program.
The output will also be redirected to the file, so solutions that get what I want "displayed" in the file are fine, however they work. (e.g., a hack that puts the input file contents directly into the output file would be fine, but only if the individual inputs end up at the right points relative to the program output, so it looks like the example above)
I can use bash script (or Python or C) to implement the solution, if it's more complicated than a single command line (but bash would be preferred, unless it's significantly easier to implement in another language)
Thanks!
Edits: What I have found out about using tee
so far...
The man page for tee
does say that giving - to tee causes it to copy again to stdout, but that just results in two copies of everything being passed as input to ./a.out
. If I run ```tee - < input.txt`` then it prints this:
one
two
three
one
two
three
and if I pipe the output of tee to a.out then all of that becomes input to a.out
So then I tried tee /dev/tty < input.txt
which does correctly direct the copy of the stream directly to the terminal while only the original copy gets redirected. So tee /dev/tty < input.txt | ./a.out
gives the correct input to a.out and I also see the inputs on the screen, but two problems arise:
1) It seems to dump the entire input at once instead of displaying as it is read from the stdin stream. In other words, the output looks like this:
prompt> one
two
three
prompt> prompt>
2) If I redirect the output of a.out to a file (which is the ultimate goal) then the stream that was sent to /dev/tty
is still printed to the terminal and does not appear in the output file. So the command tee /dev/tty < input.txt | ./a.out > output.txt
will write this into the output.txt file:
prompt> prompt> prompt>
and this will still be displayed on the terminal:
one
two
three
(It kinda makes sense if you think about what sending it to /dev/tty
actually means...)
So one thing I thought about trying was having tee send it directly to the output file, like tee -a output.txt | ./a.out >> output.txt
and that does send everything to the output file, so it fixes (2) above but (1) is still a problem and the file contents looks like:
one
two
three
prompt> prompt> prompt>