1

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

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could you elaborate on "as you should"? I'd like to know whether subprocess is a good choice for my situation. I'm not asking for a comprehensive essay, but knowing why you think the OP should use subprocess in his situation would help.
@LarsH I just meant that subprocess is now the preferred module for any kind of process-calling, rather than commands, os.popen, os.system, etc.
1

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

Comments

0

You can use os.system open a terminal window for that, such as gnome-terminal, to run the bash script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.