I have a program that outputs to stdout and stderr, but does not use them correctly. Some errors go to stdout, some go to stderr, and non error stuff goes to stderr and it prints way a lot of information about stdout. To fix this, I want to make a pipeline:
- Save all output
$cmd(from stderr and stdout) to a file $logfile(do not print it on the screen). - Filter out all warnings and error messages on stderr and stdout (from warning | error to an empty line) and color only the words "error" (redirect the output to stderr).
- Save the output of step 2 to a file
$logfile:r.stderr. - Exit with the correct exit code for the command.
So far I have this:
$!/bin/zsh
setopt no_multios
alias -g grep_err_warn="(sed -n '/error\|warning/I,/^$/p' || true)"
alias -g color_err="(grep --color -i -C 1000 error 1>&2 || true)"
alias -g filter='tee $logfile | grep_err_warn | tee $logfile:r.stderr | color_err'
{ eval $cmd } 2>&1 | filter
exit $pipestatus[1]
I tried a lot of things but can't make it work. I read "From Bash to Z Shell", a lot of posts, etc. My problems currently:
- Only stdin is included in the filter
Note. $cmdis a shell script that calls binary with a prefix /usr/bin/time -p. This seems to be causing problems with the pipelines, and so I end the command in {…}, all the output goes to the channel.
source
share