如何在python中的shell脚本中设置退出状态

 RvJ手机用户2997047695f 发布于 2023-02-13 11:43

我想获得在从python调用的shell脚本中设置的退出状态.代码如下

python脚本.

result = os.system("./compile_cmd.sh")
print result

(compile_cmd.sh)

javac @source.txt
#i do some code here to get the no of compilation errors
if [$error1 -e 0 ]
then
echo "\n********** JAVA compilation sucessfull **********"
exit 0
else
echo "\n** JAVA Compilation Error in file ** File not checked in to CVS **"
exit 1
fi

我正在运行此代码.但无论我返回什么退出状态,我得到的结果var为0(我认为它返回shell脚本是否成功运行)我知道如何获取我在shell脚本中设置的退出状态python脚本??

1 个回答
  • import subprocess
    result = subprocess.Popen("./compile_cmd.sh")
    text = result.communicate()[0]
    returncode = result.returncode
    

    从这里开始:使用Python子进程通信方法时如何获取退出代码?

    2023-02-13 11:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有