您的位置:首页 > 运维架构 > Shell

python调用linux shell脚本,并返回结果一例

2012-03-04 14:54 519 查看
#!/usr/bin/python#-*- coding:cp936 -*-import re;import os;import sys;if(__name__=="__main__"): p=re.compile('(^\s+|\s+$)'); pdigit=re.compile('^\d+'); delimiter='\t'; strdict='dict.dat'; strcontigency='contigency.dat'; str_input=str(sys.argv[1]); str_weight=str(sys.argv[2]); str_output=str(sys.argv[3]); fid_input=file(str_input,'r'); fid_output=file(str_output,'w'); keywords=[]; keywordstmp=fid_input.readlines(); for m in keywordstmp: mykey=p.sub('',m); keywords.append(mykey); for keyword in keywords: results=[]; #results.append(keyword); command=''; command='grep -n ^%s$ %s'%(keyword,strdict); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); linenum=int(temps[0])+1; command="sed -n '%dp' %s"%(linenum,strdict); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]) ;#indicate howmany docs key indexed; totalindexed=temp; #results.append(temp); command='grep -n ^%s$ %s'%(keyword,str_weight); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); linenum=int(temps[0])+2; command="sed -n '%dp' %s"%(linenum,str_weight); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]) ; temps=temp.split(' '); myclass=temps[0] #results.append(myclass); command="grep -n '^%s %s' %s"%(keyword,myclass,strcontigency); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); temp1=p.sub('', temps[1]); results=temp1.split(' '); results.append(totalindexed); #numin=m[1]; #numnotin=m[2]; #print numin; #print numnotin; #print results #s=raw_input('please enter'); #results.append(numin); #results.append(numnotin); myline=delimiter.join(results); fid_output.write(myline); fid_output.write('\n'); print myline; fid_input.close(); fid_output.close(); print '%s has finished, congratulations!'%str(sys.argv[0]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: