Shell Scripting -- Trap Weirdness
Using bash 3.2.13 (I also tried 3.1.17) I found a strange behaviour using nested traps. The following script will demonstrate the problem:
Code:
#!/bin/bash Many thanks in advance, Christian PS: Yes, I actually do need this kind of structure. Could replace it with an ugly goto but bash doesn't support that either. PPS: If you find all those echos hard to read or don't trust them, stripped down version following: Code:
foo() { |
Quote:
------------------ Steve Stites |
Quote:
PS: BTW, I don't want those traps to behave as if they were nested (whatever that means anyway). I'm just assuming that there is such a thing because the second trap which is set before the first one has finished its commands behaves differently (i.e. not at all). |
"PS: BTW, I don't want those traps to behave as if they were nested (whatever that means anyway). I'm just assuming that there is such a thing because the second trap which is set before the first one has finished its commands behaves differently (i.e. not at all)."
Nesting means that the kernel keeps a pushdown list of traps. When the latest trap is executed then it is removed and the next youngest trap becomes operative. Nesting does not happen for the trap command. When you issue a trap for a particular SIGSPEC then any previous trap for that SIGSPEC ceases to exist. So only the latest trap command is ever operative in your program. "But shouldn't the exit command in the second trap already exit the script (and not only the function, which it apparently doesn't do either BTW)?" No the second trap command does not contain any command argument. "And if it doesn't how do I exit the script no matter where I am?" Use the exit command as the ARG parameter in the trap command that is currently valid. See: man trap -------------------- Steve Stites |
Quote:
With "second trap" I mean the second one that gets called, not the second one to appear in the code. Sorry for not being exact on this matter. So let me rephrase my question: Shouldn't the exit command in the Code:
trap 'exit' 2 And what do you mean there's no ARG parameter? Calling trap without any parameters just outputs the current signal command mapping. It's there for debugging purposes only which is why I omitted it in the second version of the script. If that's what you meant then unfortunately that won't resolve the issue. |
Quote:
will exit the code on the first press of Ctrl-C. ------------------ Steve Stites |
Quote:
|
Code:
function trap1 { Code:
$ traptest.sh_ On the other hand, if things are changed somewhat... Code:
function trap1 { Code:
$ traptest.sh_ A third test script.. to test the viability of using the return code always: Code:
function trap1 { Code:
$ traptest.sh Again.. this has a score of 'Windows' in lameness. Still, for my particular script, I now know I can check the return code of `cat <named pipe>` to see if it had any data to read. At least, I think I can. Hope this clears up something. -DrkShadow |
That was very enlightening indeed. Not good news, but illuminative :) As the Posix standard doesn't seem to regulate this behaviour, do you think a bug report might help?
|
I've done loads of stuff with traps in the past.
They are awkward and not to be trusted. You can never ever get really fine control or robust exception handling in a sh, bash, or ksh script. I've tried and tried. I keep away from them now if i can help it. |
All times are GMT -5. The time now is 07:36 PM. |