| | |
python and shell scripting
![]() |
•
•
Join Date: Aug 2005
Posts: 18
Reputation:
Solved Threads: 0
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!
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!
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:
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.
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:
Python Syntax (Toggle Plain Text)
import os stdout_handle = os.popen("echo something", "r") text = stdout_handle.read()
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.
Vi veri veniversum vivus vici
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
What commands are you trying to get output with. I have not tested alot, but I get output with both cat and grep
If you want to use the output for a condidtion or some other reason, you can use the commands.getoutput() function
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
Python Syntax (Toggle Plain Text)
>>> 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
Python Syntax (Toggle Plain Text)
>>> 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
Python Syntax (Toggle Plain Text)
>>> 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
![]() |
Similar Threads
- python with bash shell (Python)
- Sleep Command in UNIX Shell Scripting (Shell Scripting)
- new to shell scripting, need some help (Shell Scripting)
- how to Upload files using Linux bash Shell scripting and CGI (Shell Scripting)
- shell scripting help with backup script (Shell Scripting)
- "shell scripting tutorials?" (Shell Scripting)
Other Threads in the Python Forum
- Previous Thread: New Problem with input output in Tk
- Next Thread: sourcing a python config file?
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib





