python and shell scripting

Reply

Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

python and shell scripting

 
0
  #1
Aug 9th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: python and shell scripting

 
0
  #2
Aug 9th, 2005
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:
  1. import os
  2. stdout_handle = os.popen("echo something", "r")
  3. 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.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: python and shell scripting

 
0
  #3
Aug 9th, 2005
What commands are you trying to get output with. I have not tested alot, but I get output with both cat and grep
  1. >>> os.system('cat testfile')
  2. this is
  3. the output
  4. I am getting, I think you
  5. even get
  6. error output
  7. 0
  8. >>> os.system('cat testfile | grep error')
  9. error output
  10. 0

If you want to use the output for a condidtion or some other reason, you can use the commands.getoutput() function
  1. >>> import commands
  2. >>> output = commands.getoutput('cat testfile')
  3. >>> print output
  4. this is
  5. the output
  6. I am getting, I think you
  7. even get
  8. 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

  1. >>> 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' ' ' """
  2. >>> os.system(command)
  3. this
  4. is
  5. the
  6. output
  7. I
  8. am
  9. getting,
  10. I
  11. think
  12. you
  13. odd
  14. get
  15. error
  16. output
  17. now the tr command is removing the vowels
  18. th s s
  19. th tp t
  20. I m g tt ng, I th nk y
  21. dd g t
  22. rr r tp t
  23. 0
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

Re: python and shell scripting

 
0
  #4
Aug 10th, 2005
thanks!
you helped me a lot!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: python and shell scripting

 
0
  #5
Aug 10th, 2005
your welcome :-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC