[Python陷阱]os.system调用shell脚本获取返回值

it2025-01-16  25

当前有shell个脚本/tmp/test.sh,内容如下:

#!/bin/bashexit 11

使用Python的os.system调用,获取返回值是:

>>> ret=os.system("/tmp/test.sh")>>> ret2816

查看Manual没有说明。网上找到解释如下:

os.system(cmd):

该方法在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码。如果我们需要获得os.system的正确返回值,那使用位移运算(或者除以256)可以还原返回值:

>>> ret/25611>>> ret>>811>>>

转载于:https://www.cnblogs.com/tuzkee/p/3151953.html

最新回复(0)