Take stdin from file but still have it show up in terminal
I have a program which requires me to input data as the program runs. Imagine it something like so:
$ ./program
Hi there. What's your name? Zambezi
What is your quest? To make a program which runs nicely
What is your favourite color? Red
...
Now, I have a number of test inputs to run my program up against. They all contain something like:
Arthur, King of the Britons
To seek the Holy Grail
...
However, some of my test scripts fail, and unfortunately it is very hard for me to decipher exactly where they failed, as my terminal looks like so:
$ ./program < arthur.txt
Hi there. What's your name?What is your quest?What is your favourite color?...
Is there a way that I can still give input to stdin
via a file, but still have the terminal appear as if I had typed it all in?
Linux Mint 16 is my OS if that matters.
Instead of using input redirection (./program < arthur.txt), which is just buffering input to your program, you should use tools just as "expect" to wait for the question and send the answers one by one.
#!/usr/bin/expect
log_user 0
spawn ./program
log_user 1
expect {
"*?"
}
send "Arthur, King of the Britons\r"
expect {
"*?"
}
send "To seek the Holy Grail\r"
expect {
"*?"
}
send "...\r"
Better examples: http://www.pantz.org/software/expect/expect_examples_and_tips.html
This is exactly what tee
is used for.
For example:
$ echo foo | tee >( grep bar )
foo
$
What happens here is tee takes stdin and copies it to stdout and pipes it out again. Just like a t joint for pipes.
Check the manpage tee(1) for more details.