How can I synchronize stderr and stdout synchronously, but print stderr only on the screen?

advertisements

This is a task that I try to do pretty often. I want to log both stderr and stdout to a log file. But I only want to print to console stderr.

I've tried with tee, but once I've merge stderr and stdout using "2>&1". I can not print stdout to the screen anymore since both my pipes are merged.

Here is a simple example of what I tried

./dosomething.sh | tee -a log 2>&1. Now I have both stderr and stdout to the log and the screen.

Any Ideas?

Based on some reading on this web site, this question has been asked. Write STDOUT & STDERR to a logfile, also write STDERR to screen

And also a question very similar here: Save stdout, stderr and stdout+stderr synchronously

But neither of them are able to redirect both stdout+stderr to a log and stderr to the screen while stdoud and stderr are synchronously written to the log file.


I was able to get this working in bash:

(./tmp.sh 2> >(tee >(cat >&2) >&1)) > tmp.log

This does not work correctly in zsh (the prompt does not wait for the process to exit), and does not work at all in dash. A more portable solution may be to write a simple C program to do it.