问题
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