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


Groups > gnu.bash.bug > #15833

Re: Protect Loop Execution with Traps

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Robert Elz <kre@munnari.OZ.AU>
Newsgroups gnu.bash.bug
Subject Re: Protect Loop Execution with Traps
Date Wed, 29 Jan 2020 13:05:32 +0700
Lines 40
Approved bug-bash@gnu.org
Message-ID <mailman.6.1580278051.2384.bug-bash@gnu.org> (permalink)
References <20200128212551.GD12574@localhost4.local> <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org> <59.1580277932@jinx.noi.kre.to>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1580278051 27610 209.51.188.17 (29 Jan 2020 06:07:31 GMT)
X-Complaints-To action@cs.stanford.edu
Cc Greg Wooledge <wooledg@eeg.ccf.org>, bug-bash@gnu.org
To Roger <rogerx.oss@gmail.com>
Envelope-to bug-bash@gnu.org
In-Reply-To <20200128212551.GD12574@localhost4.local>
X-detected-operating-system by eggs.gnu.org: GNU/Linux 3.x [generic]
X-Received-From 2001:3c8:9009:181::2
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <59.1580277932@jinx.noi.kre.to>
X-Mailman-Original-References <20200128212551.GD12574@localhost4.local> <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org>
Xref csiph.com gnu.bash.bug:15833

Show key headers only | 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