#!/bin/bash
echo hello
if test -t 1; then
    # Stdout is a terminal.
    exec >log
else
    # Stdout is not a terminal.
    exec 7>&1
    coproc tee log 1>&7
    #echo Stdout of coproc: ${COPROC[0]} >&2
    #echo Stdin of coproc: ${COPROC[1]} >&2
    #ls -la /proc/$$/fd
    exec 7>&-
    exec 7>&${COPROC[1]}-
    exec 1>&7-
    eval "exec ${COPROC[0]}>&-"
    #ls -la /proc/$$/fd
fi
echo goodbye
echo error >&2
# Released under GNU General Public License 3 by celeste:crystalfaery 2011-02-07 00:33:18+00:00
# http://www.crystalfaeries.net/linux/bin/logging_example


syntax highlighted by Code2HTML, v. 0.9.1