From owner-freebsd-questions@freebsd.org Fri Jan 15 08:41:36 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 C3FE2A83F89 for ; Fri, 15 Jan 2016 08:41:36 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from outbound-queue-2.mail.thdo.gradwell.net (outbound-queue-2.mail.thdo.gradwell.net [212.11.70.35]) by mx1.freebsd.org (Postfix) with ESMTP id 8ACD72A29 for ; Fri, 15 Jan 2016 08:41:36 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from outbound-edge-1.mail.thdo.gradwell.net (bonnie.gradwell.net [212.11.70.2]) by outbound-queue-2.mail.thdo.gradwell.net (Postfix) with ESMTP id 2AD8F552BD; Fri, 15 Jan 2016 08:41:29 +0000 (GMT) Received: from relay.gradwell.com (HELO arthur.home.qeng-ho.org) (212.11.70.4) (smtp-auth username arthur@pop3.qeng-ho.org, mechanism plain) by outbound-edge-1.mail.thdo.gradwell.net (qpsmtpd/0.83) with ESMTPA; Fri, 15 Jan 2016 08:41:28 +0000 Subject: Re: Sh-test, bug or not? To: CK , freebsd-questions@freebsd.org References: <0LwGDy-1a6CqZ3TAs-0186MD@mail.gmx.com> From: Arthur Chance Message-ID: <5698B0B6.7050000@qeng-ho.org> Date: Fri, 15 Jan 2016 08:41:26 +0000 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 In-Reply-To: <0LwGDy-1a6CqZ3TAs-0186MD@mail.gmx.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gradwell-MongoId: 5698b0b8.15227-506f-1 X-Gradwell-Auth-Method: mailbox X-Gradwell-Auth-Credentials: arthur@pop3.qeng-ho.org 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 08:41:36 -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. 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. -- Moore's Law of Mad Science: Every eighteen months, the minimum IQ necessary to destroy the world drops by one point.