2017-06-22 10 views
1

Je suis nouveau bash et que vous voulez attraper mon code de sortie du script python mon script.py ressemble:obtenir le code de sortie du script python dans le script 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:] 

mon script bash:

#!/bin/bash 
python script.py -a a1 -b a2 -c a3 
if [ $?!=0 ]; 
then 
    echo "exit 1" 
fi 
echo "EXIT 0" 

mon problème est que je reçois toujours exit 1 imprimé dans mon script bash, comment puis-je obtenir mon code de sortie python dans mon script bash?

Répondre

5

les espaces sont importants, car des arguments delimiter

if [ $? != 0 ]; 
then 
    echo "exit 1" 
fi 
echo "EXIT 0" 

ou un test numérique -ne, voir man [ pour la commande [ ou man bash pour builtin plus de détails

# 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

qui savait que vous devez attendre pour marquer répondre comme accepté (j'attends 8 min) :) – LordTitiKaka