Pipe stderr and stdout to different commands (not just to files)
As indicated by this answer at Unix SE:
MyWeirdCommand.sh
#!/bin/bash
echo "1 2 3"
echo "4 5 6" >&2
testRedirection.sh:
#!/bin/bash
(./MyWeirdCommand.sh | cut -f1 >stdout.log) 3>&1 1>&2 2>&3 | cut -f3 >stderr.log
Running yields:
stderr.log
6
stdout.log
1
In Bash, you can use process substitution to manage the extra file descriptors for you. You may find this a little neater looking than the file descriptor swap method.
command > >(process_stdout) 2> >(process_stderr)
Your command might look something like this:
/usr/bin/ldapsearch -x -LLL -b "dc=contoso,dc=com" "(objectclass=*)" -h ldap.server -v \
> >( \
gzip -c > /mnt/backups/ldap/$(date '+%Y%m%d').ldif.gz ||
logger -t ldapbackup -p local6.err error exit $?
) \
2> >( \
grep -Ev "ldap_initialize( ldap://ldap.server )|filter: (objectclass=\*)|requesting: All userApplication attributes" > "$err_log" \
)
This is how I print stdout and stderr to separate files with timestamps (piping to ts from Debian moreutils package):
(./my_little_script.pl | ts %F\ %T > out.log) 2>&1 | ts > err.log
P.S. if you don't have ts, make your own alias:
alias ts='while IFS= read -r line; do printf "%s %s\n" "$(date +%F\ %T)" "$line"; done'