Command shell access within Python

Stuart D. Gathman stuart at bmsi.com
Sun Nov 4 17:24:40 EST 2001


In article <3BE5A1A7.75C7EC9C at attglobal.net>, "Pierre Rouleau"
<pieroul at attglobal.net> wrote:

> Actually, i knew that i can execute a single program inside a command
> shell and get the return code. What i want is to execute a large set of
> commands inside the same shell process and being able to get the return
> code for exach one of the calls.
> 
> For example,  i would like to be able to do someting like:
> 
> myShell = Process('cmd')
> error = myShell.execute('make myproject') if (error == 0) :
>     myShell.execute('ls')
>     myShell.execute('pj ci makefile')
> 
> 
> I want to be able to issue a whole list of line commands whithout having
> to create a new process on every one of them.

When you run commands in a traditional shell script, it creates a new process
for every one of them!

The equivalent of the above is:

#!/usr/bin/python
import os
error = os.system('make myproject')
if error == 0:
  os.system('ls')
  os.system('pj ci makefile')

The equivalent bourne shell would be:

#!/bin/sh
make myproject
if [ "$?" == 0]; then
  ls
  pj ci makefile
fi

The number of 'fork' and 'exec' calls is exactly the same for both script
languages.  It is very convenient to run subprocesses in bourne shell,
however bourne quickly becomes impenetrable when there is a lot of
internal processing.  While python syntax is somewhat clumsier than bourne
for running external programs, internal processing is quite elegant.

In both cases, a new process is created for each command.

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.



More information about the Python-list mailing list