Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

shell

unix

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?

like image 669
user2831237 Avatar asked Dec 03 '13 09:12

user2831237


1 Answers

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?

like image 199
cptPH Avatar answered Oct 07 '22 23:10

cptPH