Python code to replace shell scripts

Edvard Majakari edvard+news at majakari.net
Thu Dec 11 14:54:28 EST 2003


"Daven Nair" <nair_260 at hotmail.com> writes:

>
> 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

See 'pydoc commands'. You could probably do with

pattern = "<grep pattern>"
cmd_status, count = commands.getstatusoutput("ps -ef|grep -c %s" % pattern)

for i in range(count):
    # do something count times

Note: you don't need wc -l after grep, because grep has '-c' switch. 

Note2: not tested, some typos etc may be present.

-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";




More information about the Python-list mailing list