shell脚本形如:
# test.sh
#!/bin/sh
echo 1
sleep 3
echo 2
sleep 3
echo 3
python程序:
import os
cmd = 'sh test.sh'
ret = Popen(cmd, stdout=PIPE, shell=True)
num = ret.stdout.read()
if ret == 1:
print "test 1"
elif ret == 2:
print "test 2"
elif ret == 3:
print "test 3"
结果为:num的值只有1,2和3则由shell脚本直接输出
请问:如果在shell的执行过程中,实时的得到脚本打印的值,然后将该值赋给python程序中的变量,根据变量值进行不同的处理。
另一种写法:
import os
ss=os.popen("sh test.sh").readlines()
print ss
联系客服