我是bash的新手,想抓住我的python脚本退出代码
我的script.py
看起来像是:
#! /usr/bin/python
def foofoo():
ret = #do logic
if ret != 0:
print repr(ret) + 'number of errors'
sys.ext(1)
else:
print 'NO ERRORS!!!!'
sys.exit(0)
def main(argv):
#do main stuff
foofoo()
if __main__ == "__main__":
main(sys.argv[1:]
我的bash脚本:
#!/bin/bash
python script.py -a a1 -b a2 -c a3
if [ $?!=0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
我的问题是我总是在我的bash脚本中打印exit 1
,
如何在我的bash脚本中获取我的python退出代码?
答案 0 :(得分:6)
空格很重要,因为是参数分隔符
if [ $? != 0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
或数字测试-ne
,有关man [
命令的[
或有关内置更多详情的man bash
,请参阅
# store in variable otherwise will be overwritten after next command
exit_status=$?
if [ "${exit_status}" -ne 0 ];
then
echo "exit ${exit_status}"
fi
echo "EXIT 0"