I've inherited a script that does this in a function:
# IO redirection for logging.
#touch $LOGFILE
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file $LOGFILE.
touch $LOGERR
exec 7>&2 # Link file descriptor #7 with stderr.
# Saves stderr.
exec 2> $LOGERR # stderr replaced with file $LOGERR.
Later on in the function it does this:
#Clean up IO redirection
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
exec 1>&7 7>&- # Restore stdout and close file descriptor #7.
The problem is if I call this function twice (i.e. in a loop) the second iteration stops at the first set of code above without showing an error.
Why would it be doing that?
$LOGFILE
and$LOGERR
into quotes; i.e.,"$LOGFILE"
and"$LOGERR"
. And if you intend to capture all the output from the function in the$LOG
files (as opposed to just the last call), the second (third) command should be “exec >> "$LOGFILE"
” and the fifth (sixth) one should be “exec 2>> "$LOGERR"
”.) – Scott Feb 14 '13 at 18:05