Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Roger 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20200128020322.GA31704@localhost4.local> Xref: csiph.com gnu.bash.bug:15826 --rwEMma7ioTxnRzrJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I've always had a problem with Bash script (eg. for/while) loops creating h= avoc=20 upon a ctrl-c keypress. One good idea, is not to put statements (eg. rm) within the loop that could= =20 possibly create problems upon partial execution. Another idea, addressing the monkey within the room, should a trap statemen= t=20 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= )",=20 is included within the loop, however this seems to cause more problems as a= =20 value of a counter variable (i=3Di+1) cannot be easily incremented due to= =20 subshells not inherienting previously defined variables of the parent shell. Example Script:=20 #!/bin/bash declare -i i=3D0 while (( $i >=3D 0 )); do # Protect loop trap "printf 'Caught SIGINT\n'; exit" SIGINT =20 # Statements here printf "Pres CTRL+C to stop... %s\n" ${i} =09 sleep 1 =09 let "i++" done --=20 Roger http://rogerx.sdf.org/ --rwEMma7ioTxnRzrJ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEE9RWsHyMf/E7wvN52Bs6g7OKIfHUFAl4vlmcACgkQBs6g7OKI fHWVRwgAivtrInfubdNZRUK2eZ7RRvZ5Bw4yBGN5GEqk18bNjA8JS7LfGeORaeJr 7e81Vn9leNiBodJp05eG5OWycOtZt0GhKxJ/CeZi8M48StTZEHIUfeju7TImkK6K AVQTyYvRwzIq92Yfb9hFyrgBBuB1go5OrtP1otUFpczFWW2SZfbrwtC4e6J/nnNk 32E1xufgitABvHrCqcdSFYJG01hZpXIfUv376DWaX3ivQ6ipoCn1pBdyv2abQSqM gR+b73NUWfjLDXrpNhluLPgtkkBN1jLq1BRVdAw7HX6JbUePYctJag4dUvnio6Tb Zvg2t6DaxfUoEBzpZRjbYAgVBPqZiQ== =b8B3 -----END PGP SIGNATURE----- --rwEMma7ioTxnRzrJ--