script
does not apply to your specific use case. You would like to see input and output in your program in the same way as the user would see it, but without having to do it yourself.
I found Expect , which seems to be exactly what we are looking for. I do not know Tcl, but there is a Python port, pexpect
. You need to install pexpect:
wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz tar xzf pexpect-2.3.tar.gz cd pexpect-2.3 sudo python ./setup.py install
Then copy this code to the executable file:
#! /usr/bin/env python import sys, pexpect executable = sys.argv[1] infile = sys.argv[2] proc = pexpect.spawn(executable) file = open(infile) for line in file: proc.send(line) proc.sendeof() proc.expect(pexpect.EOF) print proc.before,
And then you can run it like this:
transcript ./executablefile fileforinput
My mileage sample gave me this conclusion:
Enter a number: 1 Twice your number is: 2 Enter a number: 2 Twice your number is: 4 Enter a number: 3 Twice your number is: 6 Enter a number: 0 Twice your number is: 0
Assuming I read your question correctly, this should be the exact answer you are looking for. And it works in any program without any changes.
Hope this helps!
-Jake
jakebman
source share