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 stuff to screen and at the end I return a string with:
sys.exit(myString)
In my bash script, I did the following:
outputString=`python myPythonScript arg1 arg2 arg3 `
But then when I check the value of outputString
with echo $outputString
I get everything that the Python script had printed to screen, but not the return value myString
!
How should I do this?
EDIT: I need the string because that tells me where a file created by the Python script is located. I want to do something like:
fileLocation=`python myPythonScript1 arg1 arg2 arg1`
python myPythonScript2 $fileLocation
You answered your own question. If you want to run a Python script from bash and store the output in a bash variable, be selective in what your Python script prints.
Make a "verbose" flag that you can turn off in your Python script to suppress extra text that you don't want stored in your bash variable. Don't get involved in writing valid output to
stderr
or overriding exit codes! (that's what the bad kids do...)Try something like this: