13

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?

1

3 Answers 3

11

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

1
  • Slightly simpler for something similar that I was doing: (/tmp/test.sh | gzip > /tmp/out.gz) |& gzip > /tmp/err.gz Commented Jul 21, 2020 at 21:21
29

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" \
  )
4
  • 2
    This is the correct answer. Commented Aug 29, 2013 at 18:10
  • You might want to redirect the output back to stderr if you want to keep the chain instead of redirect to a file, something like this: sh f > >(sed -e "s/^/stdout: /") 2> >(sed -e "s/^/stderr: /" >&2 ) Commented Sep 27, 2017 at 17:03
  • 1
    What is the technical name for the >(process) notation?
    – jchook
    Commented Jan 13, 2019 at 19:04
  • 2
    @jchook I use the term in the first sentence: "process substitution". Commented Jan 13, 2019 at 20:30
2

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'
1
  • This one is the simplest. Thank you! One can also use: (myscript.sh | ts "stdout: %F %T" > stdout.log) |& ts "stderr: %F %T" | tee stderr.log Commented Nov 19, 2021 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .