MokiNex
MokiNex

Reputation: 879

Return the exit number and value of a Python script in a bash script

I want to execute a python script from a bash script, and I want to store the output of the python script in a variable.

In my python script, I print some error message with the value 0 or 1

def main (): 
      if condition A :
            sys.stderr.write("FORBIDDEN commit")
            return 1
      else: return 0
sys.exit(main())

this my bash script:

I used $? to get exit code + error value from the python script

python  /var/www/svn/TEST/hooks/pre-commit-standard-codeline.py $SVNRepository $SVNTransaction
PYTHONRESULT=$?

echo $PYTHONRESULT >&2     #echo display -->FORBIDDEN commit1


if [ $PYTHONRESULT -ne 0 ];
        then
        echo -e "\n"                                                                 >&2
        echo "=====================================================================" >&2
        echo "Your commit is blocked for the following reasons:"                     >&2
        echo -e "\n"                                                                 >&2
        echo -e ${PYTHONRESULT:0}                                                              >&2
        echo -e "\n"                                                                 >&2
        echo "=====================================================================" >&2
        echo -e "\n"
        exit 1
fi

my problem is in the bash script I want to split the exit value of the python from the error message so I can trigger my results in the echo command

I tried ${PYTHONRESULT:0} but it always gives me the exit value of the python script

Upvotes: 3

Views: 733

Answers (1)

tripleee
tripleee

Reputation: 189377

You seem to be confused about what goes where. Python already writes the error message to standard error, and the return code ends up in $? in the shell.

Usually, you don't need to examine $? explicitly very often because if and while and friends do that for you behind the scenes.

Maybe all you are looking for is

if python  /var/www/svn/TEST/hooks/pre-commit-standard-codeline.py "$SVNRepository" "$SVNTransaction"; then
    : all good, do nothing
    pythonresult=0
else
    # error message from Python will already have been printed on stderr
    # use lower case for your private variables
    pythonresult=$?
    cat <<-____eof >&2
        $0: Obnoxiously long error message.
        $0: The longer you make it, the less people will read it
            and the more actually useful information scrolls off the screen.
        $0: Python result code was $pythonresult!!!!11!
____eof
fi
exit $pythonresult

If you want to capture standard error, try

if captured=$(python ... 2>&1); then
    # mostly as above
    pythonresult=0
    # etc
else
    # mostly as above
    pythonresult=$?
    # etc
    # but you can use "$captured" to show stderr from Python
    # with whatever embellishments and screaming you want
fi

This is slightly messy because it mixes standard output and standard error.

There are ways to keep them separate if needed, but your question and code look like you don't expect anything on standard output actually.

Upvotes: 3

Related Questions