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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The exit code can be found in the $? See the example:
python yourprogram.py
if [[ $? != 0 ]] ; then
dosomething
fi
回答2:
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
orwhile
condition, where its non-zero-ness is checked:if ! python yourprogram.py; then dosomething fi