2017-06-22 161 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