shell和python中互调

一.在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")


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值