2017-06-22 131 views
1

我是新来bash和想抓住我的python脚本退出代码 我script.py样子:得到python脚本的退出代码在bash脚本

#! /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" 

我的问题是,我总是得到exit 1印在我的bash脚本中, 我怎样才能在我的bash脚本中获得我的python退出代码?

回答

5

的空间是很重要的,因为有观点定界符

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" 
+0

谁知道你需要等待标记答案接受(我等待8分钟):) – LordTitiKaka