Capturing Python process's exit status in UNIX shell

橙三吉。 提交于 2019-12-13 18:50:20

问题


I'm trying to figure out how to capture a return value from a python script in a *nix terminal. I'm using Linux.

So, for clarity, I have a converter script where you pass the Python script a number (as a string), it typecasts it, converts it and returns a number using sys.exit(status_number). I understand that I am taking advantage of the return status, but it something that I would like to be able to do.

The only thing I cannot figure out is how to capture that return status in a variable. I know one can do retVal=4, but one cannot, however, expect something like retVal=python foo.py 111 to work.

How can I do this?


回答1:


1. Capturing the exit status

python foo.py 111
retVal=$?

2. Capturing both the exit status and standard output

output=$(python foo.py 111)
retVal=$?


来源:https://stackoverflow.com/questions/25728287/capturing-python-processs-exit-status-in-unix-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!