I'm making a backup script for ldap. I want the errors to go to a file in /var/log and the output to go to another file in the backup folder. Currently I'm redirecting to a temp file and then sending the temp file to the log. I'd rather do this as a 1 liner though...
/usr/bin/ldapsearch -x -LLL -b "dc=contoso,dc=com" "(objectclass=*)" -h ldap.server -v 2>>/tmp/ldaptmp.err |
gzip -c > /mnt/backups/ldap/`date +\%Y\%m\%d`.ldif.gz ||
logger -t ldapbackup -p local6.err error exit $?
cat /tmp/ldaptmp.err | grep -v "ldap_initialize( ldap://ldap.server )" |
grep -v "filter: (objectclass=\*)" |
grep -v "requesting: All userApplication attributes" >$ERR_LOG
rm -f /tmp/ldaptmp.err
Any ideas on how to redirect stderr and stdout to different pipes to condense this command into 1 line? Or is there a better way?
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.
Your command might look something like this:
As indicated by this answer at Unix SE:
MyWeirdCommand.sh
testRedirection.sh:
Running yields:
stderr.log
6
stdout.log
1
This is how I print stdout and stderr to separate files with timestamps (piping to ts from Debian moreutils package):
P.S. if you don't have ts, make your own alias: