Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15833

Re: Protect Loop Execution with Traps

From Robert Elz <kre@munnari.OZ.AU>
Newsgroups gnu.bash.bug
Subject Re: Protect Loop Execution with Traps
Date 2020-01-29 13:05 +0700
Message-ID <mailman.6.1580278051.2384.bug-bash@gnu.org> (permalink)
References (1 earlier) <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org> <59.1580277932@jinx.noi.kre.to>

Show all headers | View raw


    Date:        Tue, 28 Jan 2020 16:25:51 -0500
    From:        Roger <rogerx.oss@gmail.com>
    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


Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Protect Loop Execution with Traps Robert Elz <kre@munnari.OZ.AU> - 2020-01-29 13:05 +0700

csiph-web