python调用shell结果输出-python调用shell脚本的返回值处理

本文介绍了Python中调用Shell脚本并处理返回值的三种方法:使用os.system获取执行状态值,使用os.popen读取输出内容,以及使用commands.getstatusoutput同时获取返回值和输出。通过示例代码展示了每种方法的实现和输出结果。
摘要由CSDN通过智能技术生成

python调用shell脚本的返回值处理几种方式:

shell脚本准备 hello.sh:

#! /usr/bin/ssh

echo "hello world!"

echo "succeed";

1. 使用os.system返回执行状态值

#------------------------------------------

#一、执行shell命令的状态返回值

#------------------------------------------

v_return_status=os.system( "sh hello.sh")

print "v_return_status=" +str(v_return_status)

输出结果:

hello world!

succeed

v_return_status=0

2. 使用os.popen返回结果

无返回终端,只打印输出内容

#------------------------------------------

#二(一)、获取shell print 语句内容一次性打印

#------------------------------------------

p=os.popen("sh hello.sh")

x=p.read()

print x

p.close()

#------------------------------------------

#二(二)、获取shell print 语句内容,按照行读取打印

#------------------------------------------

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值