Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: gentoo_eshoes@tutanota.com Newsgroups: gnu.bash.bug Subject: Re: looking for consistent C-c trap behavior Date: Sat, 18 Apr 2020 00:00:59 +0200 (CEST) Lines: 95 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_74623_2130888941.1587160859300" X-Trace: usenet.stanford.edu 1587160875 19869 209.51.188.17 (17 Apr 2020 22:01:15 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash To: Eduardo Bustamante Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1587160859; s=s1; d=tutanota.com; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=oQn+lmkEXzhFDzZ44W6E57upQoP+Iib9cCPzqeJLXc0=; b=xUvRQhpaAvvkw6ZHLY6qmyNbyIBlzihcbHolIZeNPQ3gg6DyGSwUMDBy8U15tugG eMcuRBF19XuYxZTDyq0idNmEsS/Z5uhbAqS2u1DnLZLo3zUPXTPZEB52SFxJWOHJLjQ JBMoI/OQZrj5f8cCywax86Po/L7GMa52PzPoeXOCKtELySwcvPt/N4DuVxhHb4+li2H vtrpkn/STI5RyQFPZqix0hyW//+5niftQTLlfgDQPcg9B5IuxExOUZ7q72AaKP2cwl9 ND+/YwGhs/r8Inl4akrbehFEs5vSPscTS4OE/q/FxHqWrBf/grcgMM+893kLvZpvguE i55al6+q9g== In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 81.3.6.162 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: X-Mailman-Original-References: Xref: csiph.com gnu.bash.bug:16161 ------=_Part_74623_2130888941.1587160859300 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Apr 17, 2020, 22:14 by gentoo_eshoes@tutanota.com: > > > > Apr 17, 2020, 22:02 by dualbus@gmail.com: > >> On Fri, Apr 17, 2020 at 12:59 PM gentoo_eshoes--- via Bug reports for >> the GNU Bourne Again SHell wrote: >> >>> >>> I've noticed that if I trap SIGINT in a bash script, the behavior when = encountering C-c depends on whether an external command (eg. 'sleep 100') o= r a builtin command (like 'read -p') was encountered. >>> >>> I attach an example script which requires me to press C-c twice to inte= rrupt the builtin 'read -p' command, and it only works because I'm restorin= g the trap via 'trap - SIGINT' the first time. >>> >>> My goal is to have C-c interrupt and use that exit code (130 most likel= y) to exit with from script, regardless or whether or not the interrupted c= ommand in the script was an internal or external one. >>> >>> How to do? >>> Let me put some effort into it: (attached anew script, prev. was had extra = 'sleep 100' which was supposed to be commented out) To break from the builtin 'read', I have to use C-c twice: the first time I= restore SIGINT trap so that the second C-c can actually make 'read' break/= exit $ ./sigintread.bash=20 Press C-c here...^Cinterrupted sees exit code '0' ^C Maybe I should paste it rather than attach it, here: #!/bin/bash #C-c won't break the builtin read unless default SIGINT trap is restored, t= hus needing two C-c to exit interrupted() { =C2=A0=C2=A0=C2=A0 local ec=3D"$?" =C2=A0=C2=A0=C2=A0 trap - SIGINT=C2=A0 #this restores prev. behaviour, so n= ow another C-c will stop 'read -rp' =C2=A0 =C2=A0 echo "interrupted sees exit code '$ec'" =C2=A0=C2=A0=C2=A0 #=C2=A0 exit "$ec"=C2=A0 #this is needed ONLY for when '= sleep' is used and interrupted, or else it will continue running and hit 'N= ormal exit', if used with 'read' then it will exit without breaking 'read' = thus exit code is 0 instead of 130 (128+2 aka SIGINT=3D=3D2) } trap interrupted SIGINT builtin read -rp "Press C-c here..."=C2=A0 #can use 'sleep 100' instead of = this 'read...' here, for testing how the behaviour is different with an ext= ernal command instead of a builtin one. #sleep 100 #ping 127.0.0.1=C2=A0 #ping will ec=3D0 on C-c ec=3D"$?" echo echo "Normal exit sees ec=3D$ec" exit "$ec" ------=_Part_74623_2130888941.1587160859300 Content-Type: application/octet-stream; name=sigintread.bash Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=sigintread.bash IyEvYmluL2Jhc2gKCiNDLWMgd29uJ3QgYnJlYWsgdGhlIGJ1aWx0aW4gcmVhZCB1bmxlc3MgZGVm YXVsdCBTSUdJTlQgdHJhcCBpcyByZXN0b3JlZCwgdGh1cyBuZWVkaW5nIHR3byBDLWMgdG8gZXhp dAoKaW50ZXJydXB0ZWQoKSB7Cglsb2NhbCBlYz0iJD8iCgl0cmFwIC0gU0lHSU5UICAjdGhpcyBy ZXN0b3JlcyBwcmV2LiBiZWhhdmlvdXIsIHNvIG5vdyBhbm90aGVyIEMtYyB3aWxsIHN0b3AgJ3Jl YWQgLXJwJwogIGVjaG8gImludGVycnVwdGVkIHNlZXMgZXhpdCBjb2RlICckZWMnIgojICBleGl0 ICIkZWMiICAjdGhpcyBpcyBuZWVkZWQgT05MWSBmb3Igd2hlbiAnc2xlZXAnIGlzIHVzZWQgYW5k IGludGVycnVwdGVkLCBvciBlbHNlIGl0IHdpbGwgY29udGludWUgcnVubmluZyBhbmQgaGl0ICdO b3JtYWwgZXhpdCcsIGlmIHVzZWQgd2l0aCAncmVhZCcgdGhlbiBpdCB3aWxsIGV4aXQgd2l0aG91 dCBicmVha2luZyAncmVhZCcgdGh1cyBleGl0IGNvZGUgaXMgMCBpbnN0ZWFkIG9mIDEzMCAoMTI4 KzIgYWthIFNJR0lOVD09MikKfQoKdHJhcCBpbnRlcnJ1cHRlZCBTSUdJTlQKYnVpbHRpbiByZWFk IC1ycCAiUHJlc3MgQy1jIGhlcmUuLi4iICAjY2FuIHVzZSAnc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