I have a python script which calles a bash script. The bash script compiles several modules and I would like to seethe result of the compilation printed on screen while running.
Most important, anyway, is that the bash script requires a run time quite some few input from the user. How can I make my python script give stdin/stdout to the bash script?
For the moment I am using
(_stat, _resl) = commands.getstatusoutput("./myBashScript")
but in this way the user is not promped of anything while the bash is running...
Cheers
If you use subprocess (as you should!) and don't specify stdin/stdout/stderr, they'll operate normally. For example:
# lol.py
import subprocess
p = subprocess.Popen(['cat'])
p.wait()
$ python lol.py
hello
hello
catting
catting
You could also process stdout as you like:
# lol.py
import subprocess
p = subprocess.Popen(['cat'], stdout=subprocess.PIPE)
p.wait()
print "\n\nOutput was:"
print p.stdout.read()
$ python lol.py
hello
catting
^D
Output was:
hello
catting
Try the envoy library.
import envoy
r = envoy.connect('./myBashScript')
r.send('input text') # send info on std_in
r.expect('output text') # block until text seen
print r.std_out # print whatever is in the std_out pipe
You can use os.system open a terminal window for that, such as gnome-terminal, to run the bash script.
来源:https://stackoverflow.com/questions/10449201/calling-bash-from-python