2

There's a command that always prints stuff to stderr. Such information is never useful except when it doesn't write anything to stdout.

So, is there a simple shell way, tool or command to help filtering stdout and stderr coming from the command and pipe them such that former stderr output is printed to stderr only if former stdout output is empty, otherwise (when stdout is not empty) it just prints stdout?

3

You could redirect them both to temporary files and (in a script) check if anything was written to the standard output.

Something like

#!/bin/sh
mytemp=$(mktemp -d)
trap "rm -rf $mytemp" EXIT INT QUIT HUP
"$@" 2>$mytemp/error | tee $mytemp/output
[ -s $mytemp/output ] || cat $mytemp/error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.