bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: trap handler scope wrong?


From: Chet Ramey
Subject: Re: trap handler scope wrong?
Date: Sun, 05 Mar 2006 12:58:17 -0500
User-agent: Thunderbird 1.5 (Macintosh/20051201)

Phillip Susi wrote:
> I ran into something weird the other day, but I'm not sure if it's a bug
> or not since I'm a bit new to bash shell scripting.  Basically I have a
> script that has structure like this:
> 
> set -e
> trap "cat $LOGFILE" ERR
> {
>  foo
>  bar
>  baz
> } > $LOGFILE 2>&1
> 
> If an error happens inside the {} block, it looks like the ERR trap
> handler is called inside the {} context, so it's stdout is redirected to
> the log file.  I had expected it to be called in the same scope it was
> defined in so that it's stdout would not be redirected. 
> Is this a bug, or expected behavior?  If it is expected, can anyone
> suggest a way to do what I'm trying to?  The reason for this structure
> was to redirect verbose output to a log file which would normally be
> deleted from the EXIT trap handler, but if anything goes wrong, the ERR
> handler is to cat the full log file so I can see exactly what went wrong. 

The ERR trap is handled in the context in which the shell is currently
running.  If you want to display the log file on the terminal when the
ERR trap is run, you can either save the original stdout and make the
trap command (the `cat $LOGFILE') redirect its stdout there, or you can
redirect the trap command's standard output directly to /dev/tty.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                           Live Strong.  No day but today.
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]