I have a process that generates output both on stderr
and stdout
.
I need to pipe these two different commands, but I would also like to keep seeing them on the terminal.
So I tried something like this as a proof of concept:
#!/usr/bin/env bash
set -e
function generate_output() {
echo This message goes to stderr 1>&2
echo This message goes to stdout
}
generate_output \
1> >(tee <&0 >(cat > out.log)) \
2> >(tee <&0 >(cat > err.log))
cat > out.log
is a dummy command that will be replaced by something else when I figure out how to make this work.
It almost works :
$ cat err.log
This message goes to stderr
And I see the output on the terminal.
So far so good !
But :
$ cat out.log
This message goes to stdout
This message goes to stderr
Why does the "stderr" message ends up in out.log ?
What puzzles me even more is that if I remove the tee command, the log file contain the expected result (but then I loose terminal output)
#!/usr/bin/env bash
set -e
function generate_output() {
echo This message goes to stderr 1>&2
echo This message goes to stdout
}
generate_output \
1> >(cat > out.log) \
2> >(cat > err.log)