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: Re: Protect Loop Execution with Traps Date: Tue, 28 Jan 2020 15:49:32 -0500 Lines: 81 Approved: bug-bash@gnu.org Message-ID: References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="a8Wt8u1KmwUX3Y2C" X-Trace: usenet.stanford.edu 1580244578 10571 209.51.188.17 (28 Jan 2020 20:49:38 GMT) X-Complaints-To: action@cs.stanford.edu To: Greg Wooledge , 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:references:mime-version :content-disposition:in-reply-to:user-agent; bh=xRAk2017zMiCDXflpGM4Wkh5penlXlXzuVkuz4V+6bU=; b=CSVEj47sGf+Ydeu/viNQHyDUVxq5F0t8qaG8l5UMfC3T0zbS7+hnoFSz3fNKEjwX+t Cpa5/E5YhW7wN6BYktOaDa7yanWIYMTGXOvWYs6X+kIT88u4DmZAG2R5xreY2AeLErZf fCwMUwdN+xR7hqRb66A3tV1/+p3WZyxx3Ufpm11bN8gdAw4cHO/a+3eePYZOPNTnAjzN yfovMlW0iHOsJTKCC/RLP5YSvaCL4Ua3mYqKqFf8wiRomVS/cimR73Mjoe8/m3x/ITdP 8LmdCxgGrqzSz4y8C97A7lqqyMD9cUHW+lhxnFbMeAhv14ZlsX/eS72DmZst20Wkvzp4 1ceg== 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=xRAk2017zMiCDXflpGM4Wkh5penlXlXzuVkuz4V+6bU=; b=XfbZsjjsG2bX7OC6bZ+NY7xFhruH3LVwIoYTuolqR26ig+tlQ+0PH1u0xDDe/Q9iil nQ/B4I6DqewfIX7FnisJ1qupLeYvc48S4IgcbjA/YMHhHZV6g0pwlU2s5glh9UNfybcN 2+f3OGOZ49aX0Ko3bwkG8RLaNa6V6tONrr+qTDxc3kSlkO4KLuFaW6TtBJ34s5PXUmL4 xwOGMcnARk9+aAWUJU+IlkPHldFLmHvGOlJrOxIVj/hyvJ0aq5zJSrzwnvs+9nguPHyR cBwBsbbTVKRpeZ23ZHiqdDAMA/+2xbjbA8Dx4IgECH3e7labrzIXd91cOIqoqg1V6Njm ZxKQ== X-Gm-Message-State: APjAAAUg5b80IdkpEtz8jFuGIS5rmCHUuRaJl7KvzvUSZ5o4sGwhQC6A esS086xOwgG69qMy9ZAAuUtpV9+otGE= X-Google-Smtp-Source: APXvYqzl6ixcVFOesQx+0ogoFHZBtFRhghLtShGkwJW9LdJU1mnqO8b2nfe659M0duihed+nohWflg== X-Received: by 2002:ac8:1196:: with SMTP id d22mr23641188qtj.344.1580244573837; Tue, 28 Jan 2020 12:49:33 -0800 (PST) Content-Disposition: inline In-Reply-To: <20200128134935.GM1350@eeg.ccf.org> 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::82b 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: <20200128204932.GB12574@localhost4.local> X-Mailman-Original-References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> Xref: csiph.com gnu.bash.bug:15828 --a8Wt8u1KmwUX3Y2C Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable >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 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. As I slept on this, I realized the likeliness some programs are also trappi= ng=20 CTRL-C as you just explained. The programs I'm using within a loop were ffmpeg && mv (rename) after verif= ying=20 ffmpeg created a file >0 bytes. As I speculated, I was likely doing too mu= ch=20 within one loop and should likely isolate each task to it's own specific lo= op,=20 mitigating such behaviour. (The check should have also likely checked for = a=20 ffmpeg created file >=3D to the original file before renaming/moving.) In other words, it's safer to 1) Have ffmpeg process all files within it's own loop, then 2) Perform a second loop renaming files using mv. But as I re-think think this, should be good Bash scripting practice to=20 integrate a trap within each loop?=20 >Whatever program you're using inside your loop may have the same >issues. It's extremely common. Another thought occurred to me last night, no wonder they call it Bash. Whe= n=20 you execute a Bash loop, you'll likely be sure to find yourself pounding=20 "CTRL-C" on your keyboard! As far as the design/intigration of the trap within a loop, I'm guessing my= =20 initial post contained the most simplistic, readable, proper form of a trap= for=20 creating a safe quick exiting loop? -- Roger http://rogerx.sdf.org/ --a8Wt8u1KmwUX3Y2C Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEE9RWsHyMf/E7wvN52Bs6g7OKIfHUFAl4wnlYACgkQBs6g7OKI fHXV8gf/duYVZ136EruYihnDYnxs2rErdx1IrKGcXVMjrYO0sDrafgHoQPcjeS9G cBVZFJAn+b2ktXXPyuH4VnC08bNfyrzZTWQI9nl2thQ+X/tXq8aLCkWYy/u/OggE QocBo4756LmqXqw1FxLeIqAxC3J7/SMQQXNilI0Gk47xIh0Uz/LrTpLK++YndP7T JmuCTMfbS//31oO7F8qZfY22nRijyqENAOeTp3zreelM6LCPCvrcZbJ2mBm+kQ/A VzZfzVccgX3xBRQ9ayt1Bmd8BEGIVRGUVoGp8CGnUYN24zUTPr5l6L4NgHXTAA9z iKMdoxcngMlN+8dASoDgKQBKn2v/jw== =Osk+ -----END PGP SIGNATURE----- --a8Wt8u1KmwUX3Y2C--