bug-bash
[Top][All Lists]
Advanced

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

Re: [bash-bug-001] bash-2.05b potential bug with trap


From: Chet Ramey
Subject: Re: [bash-bug-001] bash-2.05b potential bug with trap
Date: Mon, 18 Aug 2003 16:33:35 -0400

> (1) Run this shell in one window
> 
> --> cut here <--
> trap '(exit 4)' term
> while true; do            
>   sleep 1                   
>   echo $?
> done                      
> --> cut here <--
> 
> (2) kill the program in another window
> 
> The program in first window should output "271" when the program is
> killed, this is required by the standard:
> 
> " 
> The value of "$?" after the trap action completes shall be the value
> it had before trap was invoked. 
> " 
> 
> but it does not. Hence I believe it is a bug.

It's not a bug.  It depends on when and to which process you send the
signal.

First, the sleep will never get the signal.  You're sending it to the
shell running the script, not to the process group.  sleep will never
be killed.  If you change the sleep 1 to something like a `sleep 5'
and explicitly kill the sleep process from another terminal, you'll
get the behavior you expect.

No matter when the shell gets the signal, $? won't change, unless you
happen to be very lucky and the shell receives the signal when it's
not waiting for sleep to exit.  Since that dominates the execution time,
you're not likely to hit it.

If the shell receives the signal while it's waiting for sleep to terminate,
it must, by the standard, wait for sleep to finish before executing the
commands associated with the trap.  Since sleep always exits with 0 status
(remember, you're not killing it), $? will rarely be non-zero.

Chet



-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live...Laugh...Love
Chet Ramey, ITS, CWRU    chet@po.CWRU.Edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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