Get exit(1) from Python in shell

2019-12-16 21:23发布

I have python program, which is running from shell. When there is an error in python, it will exit with exit(1) or something else, and I need shell program to get that answer, if there is error, run this program again.

标签: python shell
2条回答
放荡不羁爱自由
2楼-- · 2019-12-16 21:40

The exit code can be found in the $? See the example:

python yourprogram.py
if [[ $? != 0 ]] ; then 
   dosomething
fi
查看更多
倾城 Initia
3楼-- · 2019-12-16 21:52

There is nothing special about Python in this case. You capture the exit code as with any other program:

  • either you evaluate $? where you have this code directly, as in sanyi's answer, or
  • you embed the program call in an if or while condition, where its non-zero-ness is checked:

    if ! python yourprogram.py; then 
       dosomething
    fi
    
查看更多
登录 后发表回答