How to get the exit status set in a shell script in Python

python, shell, unix

Solution

import subprocess

result = subprocess.Popen("./compile_cmd.sh")
text = result.communicate()[0]
return_code = result.returncode

Taken from here: How to get exit code when using Python subprocess communicate method?

Problem

I want to get the exit status set in a shell script which has been called from Python. The code is as below Python script ``` result = os.system("./compile_cmd.sh") print result ``` File compile_cmd.sh ``` javac @source.txt # I do some code here to get the number of compilation errors if [$error1 -e 0 ] then echo "\n********** Java compilation successful **********" exit 0 else echo "\n** Java compilation error in file ** File not checked in to CVS **" exit 1 fi ``` I am running this code, but no matter the what exit status I am returning, I am getting result var as 0 (I think it's returning whether the shell script ran successfully or not). How can I fetch the exit status that I am setting in the shell script in the Python script?

Original source

Related problems