I want to write a bash
script which should call several python
scripts.
Within the python
scripts are several print messages which I am not allowed to change. I want to parse the final status of the calculation to my bash script to decide what to do next.
My plan was to build the python
file like:
import sys
print('this is just some print messages within the script')
print('this is just some print messages within the script')
sys.stderr.write('0 or 1 for error or sucessfull')
and redirect the stderr
in the bash script (but still keep the output of the print
function on the terminal)
errormessage="$(python pyscript.py command_for_redirecting_stderr_only)"
can someone help me with only redirecting the stderr
? All solutions I found will not keep the output of the print
functions (mostly people set stdout
to null).
And: If someone has a smarter (and stable) idea to hand over the result of the calculation, it would be very appreciated.
Expected Output:
pyscript.py
import sys
print('this is just some print messages within the script')
print('this is just some print messages within the script')
sys.stderr.write('0 or 1 for error or sucessfull')
bashscript.sh
#!/bin/bash
LINE="+++++++++++++++++++++++++"
errormessage="$(python pyscript.py command_for_redirecting_stderr_only)"
echo $LINE
echo "Error variable is ${errormessage}"
output when I call bash bashscript.sh
:
this is just some print messages within the script
this is just some print messages within the script
+++++++++++++++++++++++++
Error variable is 0/1
You can swap stderr and stdout and store the stderr in a variable which you can echo at the end of your script. So try something like this:
#!/bin/bash
line="+++++++++++++++++++++++++"
python pyscript.py 3>&2 2>&1 1>&3 | read errormessage
echo "$line"
echo "Error variable is ${errormessage}"
This should print your stdout as normal and print stderr at the end.