* trap does not work in a trap function? @ 2010-09-09 8:02 Dominik Vogt 2010-09-09 8:17 ` Dominik Vogt 0 siblings, 1 reply; 8+ messages in thread From: Dominik Vogt @ 2010-09-09 8:02 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 393 bytes --] The attached small script behaves in an unexpected way (at least to me): $ ./sigtest shutdown shutdown Why is the shutdown function called again by exit when the EXIT trap was just removed? This looks like the trap builtin does not properly work from within a trap handler. -- Achtung Sicherheitswarnung: GMX warnt vor Phishing-Attacken! http://portal.gmx.net/de/go/sicherheitspaket ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 8:02 trap does not work in a trap function? Dominik Vogt @ 2010-09-09 8:17 ` Dominik Vogt 2010-09-09 8:57 ` Peter Stephenson 0 siblings, 1 reply; 8+ messages in thread From: Dominik Vogt @ 2010-09-09 8:17 UTC (permalink / raw) To: zsh-users > The attached small script behaves in an unexpected way (at least to me): Hmpf, the attachment has been stripped from the mail somwhere on the way: -- BEGIN sigtest #!/usr/bin/zsh shutdown () { echo shutdown trap - EXIT exit 77 } trap shutdown EXIT shutdown -- END sigtest > $ ./sigtest > shutdown > shutdown > > Why is the shutdown function called again by exit when the EXIT trap was > just removed? This looks like the trap builtin does not properly work from > within a trap handler. -- GMX DSL SOMMER-SPECIAL: Surf & Phone Flat 16.000 für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 8:17 ` Dominik Vogt @ 2010-09-09 8:57 ` Peter Stephenson 2010-09-09 9:44 ` Mikael Magnusson 2010-09-09 12:03 ` Dominik Vogt 0 siblings, 2 replies; 8+ messages in thread From: Peter Stephenson @ 2010-09-09 8:57 UTC (permalink / raw) To: zsh-users On Thu, 09 Sep 2010 10:17:33 +0200 "Dominik Vogt" <dominik.vogt@gmx.de> wrote: > shutdown () { > echo shutdown > trap - EXIT > exit 77 > } > > trap shutdown EXIT > shutdown > -- END sigtest > > > $ ./sigtest > > shutdown > > shutdown > > > > Why is the shutdown function called again by exit when the EXIT > > trap was just removed? This looks like the trap builtin does not > > properly work from within a trap handler. The EXIT trap you removed was the one for the "shutdown" function. In zsh, EXIT is used with functions as well as scripts. You can do what you want like this: shutdown () { echo shutdown trap 'trap - EXIT' EXIT exit 77 } unless shutdown is itself called from within a function, in which case you're removing the EXIT trap for that. From the documentation for the "trap" builtin: If sig is 0 or EXIT and the trap statement is executed inside the body of a function, then the command arg is executed after the function completes. The value of $? at the start of execu- tion is the exit status of the shell or the return status of the function exiting. If sig is 0 or EXIT and the trap statement is not executed inside the body of a function, then the command arg is executed when the shell terminates. -- Peter Stephenson <pws@csr.com> Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 8:57 ` Peter Stephenson @ 2010-09-09 9:44 ` Mikael Magnusson 2010-09-09 9:56 ` Peter Stephenson 2010-09-09 12:03 ` Dominik Vogt 1 sibling, 1 reply; 8+ messages in thread From: Mikael Magnusson @ 2010-09-09 9:44 UTC (permalink / raw) To: zsh-users On 9 September 2010 10:57, Peter Stephenson <Peter.Stephenson@csr.com> wrote: > On Thu, 09 Sep 2010 10:17:33 +0200 > "Dominik Vogt" <dominik.vogt@gmx.de> wrote: >> shutdown () { >> echo shutdown >> trap - EXIT >> exit 77 >> } >> >> trap shutdown EXIT >> shutdown >> -- END sigtest >> >> > $ ./sigtest >> > shutdown >> > shutdown >> > >> > Why is the shutdown function called again by exit when the EXIT >> > trap was just removed? This looks like the trap builtin does not >> > properly work from within a trap handler. > > The EXIT trap you removed was the one for the "shutdown" function. In zsh, > EXIT is used with functions as well as scripts. > > You can do what you want like this: > > shutdown () { > echo shutdown > trap 'trap - EXIT' EXIT > exit 77 > } > > unless shutdown is itself called from within a function, in which case > you're removing the EXIT trap for that. Could you also use "SIGEXIT() shutdown" and "unfunction SIGEXIT" instead, regardless of the amount of function nesting? (It seems to work when I try it). -- Mikael Magnusson ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 9:44 ` Mikael Magnusson @ 2010-09-09 9:56 ` Peter Stephenson 2010-09-09 10:39 ` Mikael Magnusson 0 siblings, 1 reply; 8+ messages in thread From: Peter Stephenson @ 2010-09-09 9:56 UTC (permalink / raw) To: zsh-users On Thu, 9 Sep 2010 11:44:49 +0200 Mikael Magnusson <mikachu@gmail.com> wrote: > Could you also use "SIGEXIT() shutdown" and "unfunction SIGEXIT" > instead, regardless of the amount of function nesting? (It seems to > work when I try it). What's supposed to happen is the trap, however it is set, is saved and unset during the function, then restored afterwards (before the scope in which the function's EXIT trap is run, hence the workaround). So this isn't supposed to work. I get the following: % TRAPEXIT() { echo This is the exit trap; } % fn() { unfunction TRAPEXIT; } % fn fn:unfunction: no such hash table element: TRAPEXIT % functions TRAPEXIT TRAPEXIT () { echo This is the exit trap } % exit This is the exit trap -- Peter Stephenson <pws@csr.com> Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 9:56 ` Peter Stephenson @ 2010-09-09 10:39 ` Mikael Magnusson 0 siblings, 0 replies; 8+ messages in thread From: Mikael Magnusson @ 2010-09-09 10:39 UTC (permalink / raw) To: zsh-users On 9 September 2010 11:56, Peter Stephenson <Peter.Stephenson@csr.com> wrote: > On Thu, 9 Sep 2010 11:44:49 +0200 > Mikael Magnusson <mikachu@gmail.com> wrote: >> Could you also use "SIGEXIT() shutdown" and "unfunction SIGEXIT" >> instead, regardless of the amount of function nesting? (It seems to >> work when I try it). > > What's supposed to happen is the trap, however it is set, is saved and > unset during the function, then restored afterwards (before the scope in > which the function's EXIT trap is run, hence the workaround). So this > isn't supposed to work. I get the following: > > % TRAPEXIT() { echo This is the exit trap; } > % fn() { unfunction TRAPEXIT; } > % fn > fn:unfunction: no such hash table element: TRAPEXIT > % functions TRAPEXIT > TRAPEXIT () { > echo This is the exit trap > } > % exit > This is the exit trap Ah, I had SIG instead of TRAP which obviously didn't work, which had the side effect of looking like it worked ;). -- Mikael Magnusson ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 8:57 ` Peter Stephenson 2010-09-09 9:44 ` Mikael Magnusson @ 2010-09-09 12:03 ` Dominik Vogt 2010-09-09 13:41 ` Peter Stephenson 1 sibling, 1 reply; 8+ messages in thread From: Dominik Vogt @ 2010-09-09 12:03 UTC (permalink / raw) To: zsh-users > > > Why is the shutdown function called again by exit when the EXIT > > > trap was just removed? This looks like the trap builtin does not > > > properly work from within a trap handler. > > The EXIT trap you removed was the one for the "shutdown" function. In > zsh, > EXIT is used with functions as well as scripts. > > You can do what you want like this: > > shutdown () { > echo shutdown > trap 'trap - EXIT' EXIT > exit 77 > } > > unless shutdown is itself called from within a function, in which case > you're removing the EXIT trap for that. Understood. But then, what is the localtraps option good for? I thought it was meant to do exactly what you describe above. Ciao Dominik ^_^ ^_^ -- GMX DSL SOMMER-SPECIAL: Surf & Phone Flat 16.000 für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: trap does not work in a trap function? 2010-09-09 12:03 ` Dominik Vogt @ 2010-09-09 13:41 ` Peter Stephenson 0 siblings, 0 replies; 8+ messages in thread From: Peter Stephenson @ 2010-09-09 13:41 UTC (permalink / raw) To: zsh-users On Thu, 09 Sep 2010 14:03:41 +0200 "Dominik Vogt" <dominik.vogt@gmx.de> wrote: > But then, what is the localtraps option good > for? I thought it was meant to do exactly what you describe > above. The "special" traps, the ones that don't map directly to signals, are handled differently from others; in particular because of its relationship with functions, TRAPEXIT is effectively always local. (The other special traps, SIGZERR and SIGDEBUG, don't have this kind of specialness, but they have others.) -- Peter Stephenson <pws@csr.com> Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2010-09-09 13:41 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2010-09-09 8:02 trap does not work in a trap function? Dominik Vogt 2010-09-09 8:17 ` Dominik Vogt 2010-09-09 8:57 ` Peter Stephenson 2010-09-09 9:44 ` Mikael Magnusson 2010-09-09 9:56 ` Peter Stephenson 2010-09-09 10:39 ` Mikael Magnusson 2010-09-09 12:03 ` Dominik Vogt 2010-09-09 13:41 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).