我是 Bash 新手,想要捕获我的 Python 脚本退出代码。
我的
script.py
看起来像:
#! /usr/bin/python
def foofoo():
ret = # Do logic
if ret != 0:
print repr(ret) + 'number of errors'
sys.exit(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 退出代码?
空格很重要,因为它们是参数分隔符:
if [ $? != 0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
或数字测试
-ne
。请参阅 man [
了解 [
命令或 man bash
了解内置更多详细信息。
# Store in a variable. Otherwise, it will be overwritten after the next command
exit_status=$?
if [ "${exit_status}" -ne 0 ];
then
echo "exit ${exit_status}"
fi
echo "EXIT 0"