0

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
some_name.py
  • 625
  • 4
  • 13
  • Maybe I am overseeing something but they in that proposed answer there is exactly that case missing when you want redirect stderr into a variable and keep stdout on the terminal – some_name.py Aug 09 '19 at 13:55
  • This has been answered [here](https://stackoverflow.com/questions/13299317/io-redirection-swapping-stdout-and-stderr) – user1934428 Aug 09 '19 at 14:02

1 Answers1

2

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.

tripleee
  • 139,311
  • 24
  • 207
  • 268
stani
  • 36
  • 3
  • Thank you for the answer! Unfortunately the variable $errormessage is empty when I run the script. – some_name.py Aug 09 '19 at 14:16
  • Sorry, you have to remove the parentheses around (python pyscript.py 3>&2 2>&1 1>&3), my bad – stani Aug 09 '19 at 14:19
  • Somehow Its still empty... but errormessage=$(python pyscript.py 3>&2 2>&1 1>&3) works... thanks a lot – some_name.py Aug 09 '19 at 14:27
  • `read` at the end of a pipe may run in a subshell. This is a FAQ; https://stackoverflow.com/questions/13763942/why-piping-input-to-read-only-works-when-fed-into-while-read-construct – tripleee Aug 09 '19 at 14:39