pythonbashreturn-code

Take python exit status and use it in bash command


I have a Python script that returns a status when it finishes (but not through an output).

sys.exit(status)

And I have to run a bash command that uses that status, the execution is something like:

command --host=xxx --service="xxx" --message="`myscript.py`" -s "ip" -u "user" -p "pass" " --returncode=0

Is there any way to take the return code directly from the "sys.exit(status)"?

I tried changing --returncode=0 to --returncode=$? but it does not work.


Solution

  • After the script exits, bash has the exit code in variable $?. But invoking another command would change that exit code. So you should save it to a variable right after the script returns.

    You could write something like:

    python3 script.py
    status=$?
    command --returncode=$status