Hello everyone!
I want to use bash from python, however the os.system(command) seems to act only on one command at a time.

I want to use complete commands, also containing control flow statements, like If and While, and to call them from python.

something like this code in bash:

T1="foo"
T2="bar"
if [ "$T1" == "$T2" ]; then
echo even
else
echo not even
fi

I tried to use os.system and separate the commands with ';' , and it works only on simple one line commands, but not on control flow commands.

I also want to get the command output back to my python program somehow. the os.system function returns the exit status of the command but not its output.

any suggestions?
thanks!

Recommended Answers

All 6 Replies

Hi Avner .H.,

I can't help you with your first problem, other than to say "why not put the Bash code in a Bash script?" which I guess is not an option for you, or else you would have done it.

However, I can help with the second item: grabbing STDOUT from the execution of external commands. Rather than use os.system(), use os.popen(), like so:

import os
stdout_handle = os.popen("echo something", "r")
text = stdout_handle.read()

At this point, the variable 'text' will contain any data that `echo something` sent to STDOUT (namely, the string "something"). Not sure how this works with STDERR, though - you'll have to do some digging to figure that out.

Hope that helps!

EDIT: also, with respect, why not just write the whole thing in Python? Or in Bash, if the project is simple enough?

In my limited experience, Frankenscripts are great as quick and dirty solutions to one-time problems, but for a larger project or a re-occurring problem it's better to stick to one programming paradigm. And truly, I don't know that it's worth learning how to do these kinds of programmatic gymnastics when the whole thing can likely be written in Python.

Just my $0.02, anyway.

What commands are you trying to get output with. I have not tested alot, but I get output with both cat and grep

>>> os.system('cat testfile')
this is
the output
I am getting, I think you
even get
error output
0
>>> os.system('cat testfile | grep error')
error output
0

If you want to use the output for a condidtion or some other reason, you can use the commands.getoutput() function

>>> import commands
>>> output = commands.getoutput('cat testfile')
>>> print output
this is
the output
I am getting, I think you
even get
error output

as to running multiple command like a loop. I would recommend using triple quotes to confine the command. This way the python interperter will not use them as delimiters

>>> command = """ VAR=even ; sed -i "s/$VAR/odd/" testfile; for i in `cat testfile` ; do echo $i; done; echo "now the tr command is removing the vowels" ; cat testfile |tr 'aeiou' ' ' """
>>> os.system(command) 
this
is
the
output
I
am
getting,
I
think
you
odd
get
error
output
now the tr command is removing the vowels
th s  s
th    tp t
I  m g tt ng, I th nk y
 dd g t
 rr r   tp t
0

thanks!
you helped me a lot!

your welcome :-)

We have established client-server connection in python using send() and recv() . Now we need to send a shell command(eg: ls) from 1st system to 2nd; execute it in 2nd and copy back the result to the 1st. You people out there, please suggest something...

We have established client-server connection in python using send() and recv() . Now we need to send a shell command(eg: ls) from 1st system to 2nd; execute it in 2nd and copy back the result to the 1st. You people out there, please suggest something...

Hijacking an old thrtead is not the way to go. If you really want help, start a new thread with a good descriptive title.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.