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


Groups > gnu.bash.bug > #15826

Protect Loop Execution with Traps

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Roger <rogerx.oss@gmail.com>
Newsgroups gnu.bash.bug
Subject Protect Loop Execution with Traps
Date Mon, 27 Jan 2020 21:03:22 -0500
Lines 73
Approved bug-bash@gnu.org
Message-ID <mailman.243.1580218781.2185.bug-bash@gnu.org> (permalink)
References <20200128020322.GA31704@localhost4.local>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="rwEMma7ioTxnRzrJ"
X-Trace usenet.stanford.edu 1580218782 31165 209.51.188.17 (28 Jan 2020 13:39:42 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=+j8w4D6NveTmwSs2y13pxaE0jfZCoF1ftpCRrzu/w8o=; b=c9fG8eIsILxD+OlVBu6FWojlXmjph6BM389+YhbzYPmsziV1XElLOZ0fz+7R35CzrE OuwaKgJEEPIejvjD6+QsVrzZJ71kzRi5V3Zz5dgXoQHcXdQn0kZasM+0igArgtVWbMt3 JeOZYVb9tIZi7p6DYsNxJqTyniOEJ38lUk4SGReUrtetYnoCW9qMiemOkjU9mVYYgEUH b3nQAl8xQxYe5KKURJMTxyqaLPUnUVW+jkB7etDgfeTcBEiRTfCWNdSootoNulr+LXZ6 CB320Cp03C2bGCFurBr73sAjeSeT9UHqon1pihdXasUc2vRJwHlbr+2Zc35bnxeO3Y/a Sdcg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=+j8w4D6NveTmwSs2y13pxaE0jfZCoF1ftpCRrzu/w8o=; b=LijctnHhdfdxWMsUxk1Q0sfFcVSAxXFNaoYObwpHyaPogmgGsv/PImYVuRnKqmLiQm 7GKoHANMBlZ1TjyXLUfYOw/cX3NAY6l66Ufe4qXTcThmG6YNpCZPSQbsYdrutNOYRVOq vtq8ke8UbWFY1X6vrh8uq9VY74i+suRLDK+Zb5QP1Vtl8/+EsqGBqrX/WxdX75jtK81N 2tkWlEGfAnUkGfjd73dBvGcMKGVlU2MnNL2A3MfobH0doCCRm9GeZIz3yzxSMEpTJfnN A259rqGvrVudjpnMIV7r4mpw3Z+DXvPu6Z/900QYodc5hhYsWxYW4ePe8EV4zeLQcywL Cdhw==
X-Gm-Message-State APjAAAUJQJoOMYNeNVzdQyg9YZEhB5bv+sdeQ3ICsVEIRhFJo3TCbOS7 +NhXSRBH8g7bscItoAL6I3D5Gq1+fHk=
X-Google-Smtp-Source APXvYqylj9QTKAXw5m2gXi6n6GSZ28Cc6d7vx1vAJB7ebPUKwTkATDx8y5TliwCRXwfUTtpyTzSLtQ==
X-Received by 2002:ad4:518b:: with SMTP id b11mr20294514qvp.195.1580177004285; Mon, 27 Jan 2020 18:03:24 -0800 (PST)
Content-Disposition inline
X-PGP-Key http://rogerx.sdf.org/about/armor-F515AC1F231FFC4EF0BCDE7606CEA0ECE2887C75-20170104.asc
User-Agent Mutt/1.10.1 (2018-07-13)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2607:f8b0:4864:20::f30
X-Mailman-Approved-At Tue, 28 Jan 2020 08:39:40 -0500
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 <20200128020322.GA31704@localhost4.local>
Xref csiph.com gnu.bash.bug:15826

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

I've always had a problem with Bash script (eg. for/while) loops creating havoc 
upon a ctrl-c keypress.

One good idea, is not to put statements (eg. rm) within the loop that could 
possibly create problems upon partial execution.

Another idea, addressing the monkey within the room, should a trap statement 
always be used within for/while loops?

Or would a trap even help at all?

I've seen some examples on the Internet where a subshell, eg. "( statements )", 
is included within the loop, however this seems to cause more problems as a 
value of a counter variable (i=i+1) cannot be easily incremented due to 
subshells not inherienting previously defined variables of the parent shell.

Example Script: 


#!/bin/bash

declare -i i=0

while (( $i >= 0 )); do
	# Protect loop
	trap "printf 'Caught SIGINT\n'; exit" SIGINT
    
    # Statements here
	printf "Pres CTRL+C to stop... %s\n" ${i}
		
	sleep 1
		
	let "i++"

done




-- 
Roger
http://rogerx.sdf.org/

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


Thread

Protect Loop Execution with Traps Roger <rogerx.oss@gmail.com> - 2020-01-27 21:03 -0500

csiph-web