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


Groups > gnu.bash.bug > #15827

Re: Protect Loop Execution with Traps

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Protect Loop Execution with Traps
Date Tue, 28 Jan 2020 08:49:35 -0500
Lines 41
Approved bug-bash@gnu.org
Message-ID <mailman.244.1580219381.2185.bug-bash@gnu.org> (permalink)
References <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1580219381 31391 209.51.188.17 (28 Jan 2020 13:49:41 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org
To Roger <rogerx.oss@gmail.com>
Envelope-to bug-bash@gnu.org
Mail-Followup-To Roger <rogerx.oss@gmail.com>, bug-bash@gnu.org
Content-Disposition inline
In-Reply-To <20200128020322.GA31704@localhost4.local>
User-Agent Mutt/1.10.1 (2018-07-13)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From 139.137.100.1
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 <20200128134935.GM1350@eeg.ccf.org>
X-Mailman-Original-References <20200128020322.GA31704@localhost4.local>
Xref csiph.com gnu.bash.bug:15827

Show key headers only | View raw


On Mon, Jan 27, 2020 at 09:03:22PM -0500, Roger wrote:
> I've always had a problem with Bash script (eg. for/while) loops creating havoc 
> upon a ctrl-c keypress.

What's *in* the loop?  It matters.

Consider the following two scripts:

==================================
#!/bin/bash
while true; do
  echo "Zzz"
  sleep 1
done
==================================

==================================
#!/bin/bash
while true; do
  # Assuming Linux
  ping -c 1 8.8.8.8
done
==================================

If you run these, and try to kill them with Ctrl-C, you may find that
the first one behaves perfectly (stops when you ask), and the second
one does not.  It may take several tries to kill the second one.  You
might have better luck suspending it with Ctrl-Z first, then killing
the shell only, then fg'ing the job.

See <https://www.cons.org/cracauer/sigint.html> for details.

In a nutshell, ping(8) is a misbehaving program.  It sets a SIGINT
trap (so that it can report statistics when you interrupt it), but
it doesn't exit as a result of the signal.  The shell sees that the
program didn't exit due to SIGINT, and decides that it (the shell)
should not exit either.

Whatever program you're using inside your loop may have the same
issues.  It's extremely common.

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


Thread

Re: Protect Loop Execution with Traps Greg Wooledge <wooledg@eeg.ccf.org> - 2020-01-28 08:49 -0500

csiph-web