bug-bash
[Top][All Lists]
Advanced

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

Re: Protect Loop Execution with Traps


From: Robert Elz
Subject: Re: Protect Loop Execution with Traps
Date: Wed, 29 Jan 2020 13:05:32 +0700

    Date:        Tue, 28 Jan 2020 16:25:51 -0500
    From:        Roger <address@hidden>
    Message-ID:  <20200128212551.GD12574@localhost4.local>

  | Wow, " trap 'trap INT; kill -INT $$' INT "  not easily readable for me.

You can often help with things like that by reformatting

                trap '
                        trap - INT
                        kill -s INT $$
                ' INT

and (with all respect to Gred) please avoid archaic uses, and use the
commands as they're currently specified, while "trap - INT" and "trap INT"
do the same thing, the former is the standard way, similarly for
"kill -INT ..." and "kill -s INT ..." the latter is the modern version.

  | A trap calling kill inside of a trap.

Quite common.   Try to avoid recursive traps however, those will blow
your stack and result in a core dump from bash if you're not careful.

  | I'm thinking, put "trap INT; kill -INT $$" inside of it's own function,

That's acceptable, sometimes perferable, sometimes just more complexity for
no real benefit (makes code harder to understand).

  | This trap stuff has me pulling out my hair,

traps can do that.   In general, any programming dealing with async
events is much trickier to get right than simple synchronous programming.
At least traps are only ever executed between commands, which avoids
lots of otherwise potentially impossible to deal with situations (the
signal that invokes the trap might have occurred anywhere).

kre





reply via email to

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