Here's a small Python 2 program that will do this, provided that your target program ( foo ) will produce one line for each line of input:
#!/usr/bin/env python import sys, subprocess sp = subprocess.Popen(sys.argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE) for input_line in sys.stdin: print('Input: ' + input_line.rstrip('\r\n')) sp.stdin.write(input_line) sp.stdin.flush() output_line = sp.stdout.readline() print('Output: ' + output_line.rstrip('\r\n'))
If you save this as tee.py , you can test it with
echo -e '123\n321' | tee.py cat -
for a general reproducible test, or
echo -e '123\n321' | tee.py foo
for your specific example.
PS: if you want it in Python 3, you need to change two lines:
sp.stdin.write(input_line.encode('utf-8'))
and
output_line = sp.stdout.readline().decode('utf-8')
source share