9

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.

13

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

  • Once you got the above working, you could evolve it into an expect script that knows what prompts your program issues, and knows to feed your program the third input line in response to the "What is your favorite color?" question — and then have the expect script read the arthur.txt file (or any other specified, appropriately structured, file) to get those inputs, rather than having them hard-coded in the script. – G-Man Jun 28 '15 at 9:15
7

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.

  • 1
    Are the messages guaranteed to appear in the correct order, though? Even ignoring buffering, this is essentially two programs running concurrently and trying to write onto the same screen. – Federico Poloni Jun 7 '15 at 12:44
  • 1
    How does one use this in my scenario though? I tried reading the manpages, but there seems to be a lot more to this tool than just my usecase, and I wasn't quite getting how I'd use it when it comes to program.exe and arthur.txt. – Zambezi Jun 7 '15 at 20:29
  • @Zambezi cat arthur.txt | tee >( program.exe ) – gronostaj Jun 7 '15 at 22:15
  • 2
    tee was the first thing come to my mind too. But I have tried it and confirmed it does not work. The output shows up separately. In my experiment, arthur.txt's content shows up first, followed by the questions from program.exe. You might be able to get it to work with "unbuffer" of expect but I haven't had any luck. – some user Jun 8 '15 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.