943,744 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 63424
  • Python RSS
Aug 9th, 2005
0

python and shell scripting

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Aug 9th, 2005
0

Re: python and shell scripting

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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Aug 9th, 2005
0

Re: python and shell scripting

What commands are you trying to get output with. I have not tested alot, but I get output with both cat and grep
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 10th, 2005
0

Re: python and shell scripting

thanks!
you helped me a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Aug 10th, 2005
0

Re: python and shell scripting

your welcome :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jan 23rd, 2010
0

Help us with the coding

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...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
askvaj is offline Offline
1 posts
since Jan 2010
Jan 23rd, 2010
0
Re: python and shell scripting
Click to Expand / Collapse  Quote originally posted by askvaj ...
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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Restrict a specified area in a wx.window
Next Thread in Python Forum Timeline: TKinter button changing text?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC