Python code to replace shell scripts

William Park opengeometry at
Fri Dec 12 02:58:23 CET 2003

Daven Nair <nair_260 at> wrote:
> Hi,
> I would like to know if Python supports codes similar to shell scripts:
> count=`ps -ef|grep "pattern"|wc -l`
> for count in `echo $count`
>   do
>   done
> fi
> Can I export a variable say var from os.system("var=`ps -ef|grep pattern|wc 
> -l`")

No.  'os.system()' will fork a subshell, and, as you know, subshell
cannot change parent's environment.  Furthermore, your shell script is
not proper.  It should go like
    for count in `...`; do

In any case, although Python does something well, shell does most things
better.  (It's okey... I've got my helmet on.)

William Park, Open Geometry Consulting, <opengeometry at>
Linux solution for data management and processing. 

More information about the Python-list mailing list