python代替shell脚本,用于替换shell脚本的Python代码

讨论了如何在Python中使用内置模块来执行类似于Shell脚本的任务,如通过`os.system`和`commands.getstatusoutput`进行进程计数,并在循环中使用这些计数。文章指出,不需要使用`wc-l`,因为`grep`的`-c`选项可以直接返回匹配行数。
摘要由CSDN通过智能技术生成

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`")

thanks

__________________________________________________ _______________

Cell phone ?switch? rules are taking effect ? find out more here.

http://special.msn.com/msnbc/consumeradvocate.armx

解决方案count`

do

done

fi

Can I export a variable say var from os.system("var=`ps -ef|grep pattern|wc

-l`")

thanks

__________________________________________________ _______________

Cell phone ?switch? rules are taking effect ? find out more here.

http://special.msn.com/msnbc/consumeradvocate.armx

"Daven Nair" 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 = ""

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

# PGP PUBLIC KEY available Soli Deo Gloria!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值