From owner-freebsd-questions@freebsd.org Fri Jan 15 17:24:28 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 35B66A8339B for ; Fri, 15 Jan 2016 17:24:28 +0000 (UTC) (envelope-from nibbana@gmx.us) Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mout.gmx.net", Issuer "TeleSec ServerPass DE-1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9C1E01359 for ; Fri, 15 Jan 2016 17:24:27 +0000 (UTC) (envelope-from nibbana@gmx.us) Received: from mail.gmx.com ([72.251.118.219]) by mail.gmx.com (mrgmx101) with ESMTPA (Nemesis) id 0MSZ6u-1alfO83j3E-00Rcsd for ; Fri, 15 Jan 2016 18:24:19 +0100 Date: Fri, 15 Jan 2016 08:25:27 -0900 From: "CK" To: Cc: Subject: Sh-test, bug or not? Reply-To: "CK" X-Mailer: UMail v1.0 Message-ID: <0Lg5kl-1ZnW3s29Zj-00pbLI@mail.gmx.com> X-Provags-ID: V03:K0:x32XBjhaWeWhT6f0fXsY6U3hz01y3KPQwYRRv9x8eouNcBBXJsN GEt2XQan6Vk4DXdC8Gb4Gc9i6zcQe+yo4d79YFWF39SqWAR6Wee4XmnS8qPN03Qlg1JUKGR KoU5du1UwcjaSpc0ZG7YFbtx7F/Lj3RgcGyiqybbuLdTH1l+0QBLVQEGzgyIVxbYxP4pHlu m9rb2d9ACVvAbgounNZIg== X-UI-Out-Filterresults: notjunk:1;V01:K0:eM6DZCEOVyE=:AOyngSryx7bhVVCblJ+0Yt rnOwShTEc/K1EZGBoudKy890HqBJZgh6II0Kp1luu2V5Wb0CSAHJBzvGdcM9/eClhKIUAsQhU plW5jeUq3VcVYNhPR2xMx1nkJ0NDbuFVG71rxKDOEPl8Jd6CjTbQs+LIgNMhJIN5qlwLIk0aO 2kNbUpBw/wTWXt5anIvx7AcOehpWdWasgHgFteazJEIW0wIzopbgv6k3sykQHtVZACWI7FsrG 1z18uKDo//OKpc2nd/KO7xUjF4b9mA7uPObZ3ewx53Ch5NYWlfn91tsWe99fMxnIvrjyb29sc JUILudeJec92rYc4QYJLjgiarwU6666QPiWoAQyDJL0Pdsd+72Isp6cJrmLLCoFmHTF3r3vqz sNXL2gXpq/lsJHRQSilSHE/ac6Fzcb3SHgwPb7G0XS8tYfJAkUODXwhuTEwONhuyEkEhzq95X Suq/69vFfjveT69L2b3B3X6BNQkfFwP2385OeCm9BPT9opOpJ8biy0lHdxt6DRD79M7rc5Y6J JdD3rjeBuyWry0+077odSUYgvelFM338KWMx1eIdXhPanuUxxkpzo4u0eJWAFAJdDKfgJToSx cku+u2U9yi3fDYpab3z5BqSHTL8gXijhCUlISOwxg6HXABzHKr73RpBgANuJzWDjpDBHDl9Vp lWFMXUsZPnneKJ+2iwK5nSMSfB4MFOu4RnHVGuRKHeksBPmuNONTYLVKgM3sTNsbmoIiVbPQW YMRxA64WjbQaHMerXAapMpZlwc2DyV2pEmTosiIs8uPHESAGZ4Jo5TF4670= X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jan 2016 17:24:28 -0000 > On 15/01/2016 04:32, CK wrote: > > If you have authoritative knowledge on the subject, > > please state if this functionality is correct: > > > > $ [ ! "" -a "" ] && echo pass || echo fail > > pass > > $ [ ! 11 -a "" ] && echo pass || echo fail > > pass > > > > The "-a" operator binds stronger than the "!" operator. > > Intuition based on functionality in awk/C would suppose > > that the "!" operator would bind stronger than the "-a" > > operator, especially since "-a" does in fact have higher > > precedence than the "-o" operator, as in awk/C. > > > > In order to make it work as "expected", it gets ugly: > > > > $ [ ! "" -a "" ] && echo pass || echo fail > > pass > > $ [ \( ! "" \) -a "" ] && echo pass || echo fail > > fail > > > > $ [ ! 11 -a "" ] && echo pass || echo fail > > pass > > $ [ \( ! 11 \) -a "" ] && echo pass || echo fail > > fail > > > > I never noticed this in 20 years, so I don't know if it always > > worked this way, or if something changed in my upgrade from > > 4.11 to 9.3. > > -- > REPLY#1 > ####### > I think that while unexpected in comparison to other languages which > have inherited C-like operator precedence rules, this is according to > the POSIX standard for test(1). As the man page says: > > The test grammar is inherently ambiguous. In order to assure a degree > of consistency, the cases described in the IEEE Std 1003.2 > (``POSIX.2''), section D11.2/4.62.4, standard are evaluated > consistently according to the rules specified in the standards > document. All other cases are subject to the ambiguity in the > command semantics. > > and it notes that the ambiguous cases are those involving (), -a and -o. > > Your test might be more clearly expressed as: > > $ [ ! "" ] && [ "" ] && echo pass || echo fail > fail > > (although I'd recommend the -z and -n operators for testing the > emptiness / undefinedness or not of strings.) > > -- > REPLY#2 > ####### > I would never claim to be an authority, but a quick look at the test(1) > code shows that although the comments starting line 53 suggest the > grammar one would expect, there's a special case in main() for an > expression of exactly four parts starting with "!". The comment on line > 218 is relevant to your tests. > > 217 if (nargc == 4 && strcmp(*t_wp, "!") == 0) { > 218 /* Things like ! "" -o x do not fit in the normal grammar. */ > 219 --nargc; > 220 ++t_wp; > 221 res = oexpr(t_lex(*t_wp)); > 222 } else > 223 res = !oexpr(t_lex(*t_wp)); > > I presume the shells use a version of this code as well. My guess would > be that this is some sort of kludge going back to the original bourne > shell, but someone else more knowledgeable than me will have to deal > with that. In regards to Reply#1, yes, I did read that previously, but that leaves the sensibility of the cited functionality open to whatever is most sensible. And the current functionality does not seem sensible due to its apparent senseless incongruity with standard functionality in most/all other languages, in addition to making simple expressions more lengthy/tedious/ugly than necessary. Of course, these were just examples to demonstrate the functionality. In regards to Reply#2 - that sure is odd! $ [ ! "" -a "" ] && echo pass || echo fail pass $ [ ! "" -a "" -a "" ] && echo pass || echo fail fail Haha ... !!! You hit the nail on the head! This is terrible. Thank you for making that insightful effort to find the real reason for this functionality - this sure is a strange exception. May you be free from danger, happy, healthy, and loving with ease. Submitted 21 hours ago by unixygirl, theravada [/r/Buddhism] :)