Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
I’m still not 100% sure what you mean. (A car stops when the driver steps on the brake, and it stops when it runs out of fuel –– do you see the difference?) More importantly, does my correction fix the problem? If not, what have you done to debug it? (Other suggestions: put $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
add comment

migrated from askubuntu.com Feb 7 '13 at 22:49

This question came from our site for Ubuntu users and developers.

1 Answer

I’m not exactly sure why it would “stop”, whatever you mean by that (does the script exit? hang?), I do see a problem: the last line should be exec 2>&7 7>&-.  (Also, the comment on that line should say “stderr” rather than “stdout”.)

share|improve this answer
    
By "stop" I mean the script stops executing after these lines with no further output and no error message –  DaveR9 Feb 13 '13 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.