3

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

5
  • 1
    As an aside, you should probably add a newline to the end of the stderr message you write. Commented Feb 21, 2019 at 14:05
  • could you give me an example I didn't get your point? Commented Feb 21, 2019 at 14:11
  • 2
    This question doesn't make sense. "I want to split the exit value of the python from the error message". They're already separate. The exit value is $?, and the error message was printed to stderr (and is not stored in $? nor in $PYTHONRESULT). Maybe you're confused because they both appear on the same line when you execute your bash script? Commented Feb 21, 2019 at 14:13
  • @jamesdlin thank you for your explication. in that point how can I print the stderr message in case the exit value is 1 inside the condition IF Commented Feb 21, 2019 at 14:18
  • I mean sys.stderr.write("FORBIDDEN commit\n") with \n at the end. Commented Feb 21, 2019 at 14:23

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.