Filter STDERR through a pipe by swapping STDOUT and STDERR

I have a cron job, running a non verbose script, so only errors trigger a mail. The thing is, one type of error tends bug me, as this particular error doesn’t interest me, so I’d like to filter it.

grep -v comes to mind, but a pipe only handles one channel, STDOUT (&1). How do I solve this?

Simple. Swap STDERR en STDOUT and filter the error in a subshell, then swap them again. Swapping STDERR and STDOUT is quite simple, when you know it. Just make some Hanoi kind of move, using an extra file descriptor &3:

3>&1 1>&2 2>&3

Swap STDERR to &3, STDOUT to STDERR, &3 (=STDERR) to STDIN.

Combined, this would make me run instead of /usr/bin/$COMMAND $PARAM the following, as defined in crontab:

(/usr/bin/$COMMAND $PARAM 3>&1 1>&2 2>&3 | grep -v $uninteresting_error ) 3>&1 1>&2 2>&3

Don’t you love it when a plan comes together?

Categories: All 0 comments