开发者

Console output of a program including redirected stdin?

I have a command line program that I'm passing redirected input to, and piping to a file:

./program < some_input_file > some_output_file

This obviously writes the output not including the redirected input. Is there some simple way to get a log of the program, including the redirected input that I've passed in?

I'm open to using an external program or script - I'm running this on bash/linux开发者_Go百科.

[EDIT]

I'm looking for a way to get the output interleaved - as if the program was run in a terminal, with the input file just typed in manually


The following is an example of how you can do it:

> cat input.txt
asdf
qwer
zxcv
> tee output.txt < input.txt | cat >> output.txt 
> cat output.txt
asdf
qwer
zxcv
asdf
qwer
zxcv

Just replace cat in the above with your program and you should be good. Now if you want it interleaved then you have to do something different:

> while read line
  do
      echo $line >> output.txt
      echo $line | cat >> output.txt
  done < 'input.txt'
> cat output.txt
asdf
asdf
qwer
qwer
zxcv
zxcv

again replacing cat with your shell script.


If your program prints some kind of prompt before reading its next input, you can use expect to interact with it. Your expect script can print each input line as it's read and send it to the program once it sees the prompt. That gives you correct, interleaved output without running your program once per line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜