I have an interactive program that runs stdin and stdout. I need to create wrapper that will send X to it\'s stdin, check that it prints Y and then redirects wrapper\'s stdin an
Assuming X and Y are files, and that you can invoke the program more than once:
#!/bin/bash
test "`program <X`" = "`cat Y`" && program
Or, to fail more verbosely:
#!/bin/bash
if [[ `program <X` != `cat Y` ]]; then
echo -e "Assertion that input X produces Y failed, exiting."
exit 1
fi
program
If you only invoke the program once, Expect is a much simpler alternative than reassigning standard file I/O on the fly.
You can overwrite the sys module's stdin and stdout
import sys
sys.stdin, sys.stdout = wrapper.stdin, wrapper.stdout
These need to be file objects opened for reading and writing respectively. The original stdin and stdout are found at
sys.stdin, sys.stdout = sys.__stdin__, sys.__stdout__
I'm a little confused as to what exactly you're trying to achieve; as I understand you wish to:
If this is the case you want to do this:
exec()
s and executes thh target program.exec()
into the target program.If this is correct, I can provide a ~30 line C program, or ~10 line Python program that achives this.
Expect is made for automating the running of other programs - essentially you write something like, in plain text,
Start this program. When it prints out the word "username", send it my username. When it sends "password", send it my password.
It's really great for driving other programs.