一.在shell脚本中调用python脚本,执行结果给予赋值
#!/bin/bash
#在shell中掉用python脚本执行结果赋值给key
key=`python /usr/local/pythonstudy/print.py`
a=$key
#对key进行过滤
b=`echo $key | awk -F\? '{print $3}'`
echo $b
二.3种方法python中调用shell脚本以及shell命令
#!/usr/bin/python
import os
import commands
def getstatusoutputfun():
print 'getstatusoutput fun.....'
"""a是状态, b结果"""
a,b =commands.getstatusoutput('~/demo.sh')
print a
print b
def popenfun():
print 'popen fun .....'
str=os.popen("ifconfig").read()
print str
"""
a=str.split('\n')
for b in a:
print b
"""
def sysfun():
print 'system fun......'
os.system('~/demo.sh')
def test():
getstatusoutputfun()
popenfun()
sysfun()
test()
a=os.system("ls"): 命令的返回值(a)是系统命令的返回值。结果是由子shell执行的,显示在屏幕上,无法捕捉。
a=os.popen("ls","r").read():结果可以赋于一变量,便于程序的处理。注read()的结果有"\n",用strip()加上readlines()可以得到输出。
commands.getoutput("date")
commands.getstatusoutput("date")