> -----Original Message----- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of raocheng > Sent: Friday, January 18, 2008 6:31 AM > To: python-list@python.org > Subject: How to use only a sub shell to execute many commands in python > > Please see the following code. > Suppose I have many shell commands to be executed. And I don't want to > fork a sub shell for each command(eg: status,output = > commands.getstatusoutput(cmd)) because it is too expensive. I want to > use only one sub shell to execute all these commands and want to get > each command's output. How can I accomplish this task ? Thanks in > advance. > > =========================================== > #!/usr/bin/env python > import os > fi, fo = os.popen2( > ''' > while read line > do > eval $line > done > ''', 't') > > #Suppose I have many commands to execute, but I don't want to fork a > sub shell for each command > cmds = ['date','uptime','pwd','ls -rltF','who'] > > for cmd in cmds: > #pseudocode > fi.executeCmd(cmd) > output = fo.readResult() > > print output
Have each command write to a unique temp file. Create temp files in python cmd = 'date > /tmp/date_temp 2>&1 ; uptime > /tmp/uptime_temp 2>&1; ...' execute cmd for file in tempfiles: ... You can also get the return value of each command cmd = 'date > /tmp/date_temp 2>&1; echo $? >> /tmp/date_temp; uptime > /tmp/uptime_temp 2>&1; echo $? >> ...' ***** The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622 -- http://mail.python.org/mailman/listinfo/python-list