-a vs -e
According to the man page, [ -a ] and [ -e ] should have the same behaviour. -a file True if file exists. ... -e file True if file exists. $ touch stuff.txt $ [ -a stuff.txt ]; echo $? 0 $ [ -e stuff.txt ]; echo $? 0 $ [ ! -e stuff.txt ]; echo $? 1 $ [ ! -a stuff.txt ]; echo $? 0 # what? Both these version show the exact same behaviour. Fedora 18, bash 4.2.45(1)-release OSX, 3.2.48(1)-release Kurt
Re: -a vs -e
On Wed, Sep 04, 2013 at 10:06:16PM -0700, kneuf...@gmail.com wrote: > $ [ ! -e stuff.txt ]; echo $? > 1 > $ [ ! -a stuff.txt ]; echo $? > 0 # what? > > Both these version show the exact same behaviour. > > Fedora 18, bash 4.2.45(1)-release > OSX, 3.2.48(1)-release Also occurs in 4.3.0-beta.
Re: -a vs -e
On 09/04/2013 11:06 PM, kneuf...@gmail.com wrote: > According to the man page, [ -a ] and [ -e ] should have the same behaviour. Not a bug. -a behaves like -e only when it is unambiguously parsed as a unary operator, because it is bash extension as a unary operator. > > -a file > True if file exists. > ... > -e file > True if file exists. > > $ touch stuff.txt > $ [ -a stuff.txt ]; echo $? > 0 > $ [ -e stuff.txt ]; echo $? > 0 > $ [ ! -e stuff.txt ]; echo $? > 1 > $ [ ! -a stuff.txt ]; echo $? > 0 # what? But _this_ is an instance where -a is parsed as a binary operator (ie. it was parsed as "\( ! \) -a \( stuff.txt \)", not "! \( -a stuff.txt \)". -a as a binary operator is required by POSIX for XSI, therefore it takes priority over -a as a unary operator as a bash extension. -- Eric Blake eblake redhat com+1-919-301-3266 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
Re: -a vs -e
On 09/05/2013 10:33 AM, Greg Wooledge wrote: > On Wed, Sep 04, 2013 at 10:06:16PM -0700, kneuf...@gmail.com wrote: >> $ [ ! -e stuff.txt ]; echo $? >> 1 >> $ [ ! -a stuff.txt ]; echo $? >> 0 # what? >> >> Both these version show the exact same behaviour. >> >> Fedora 18, bash 4.2.45(1)-release >> OSX, 3.2.48(1)-release > > Also occurs in 4.3.0-beta. Which is good, because it's not a bug. -- Eric Blake eblake redhat com+1-919-301-3266 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
Re: -a vs -e
[moving to the coreutils list, as coreutils has a bug here] On 09/05/2013 10:33 AM, Eric Blake wrote: > On 09/04/2013 11:06 PM, kneuf...@gmail.com wrote: >> According to the man page, [ -a ] and [ -e ] should have the same behaviour. > > Not a bug. -a behaves like -e only when it is unambiguously parsed as a > unary operator, because it is bash extension as a unary operator. > >> >> -a file >> True if file exists. >> ... >> -e file >> True if file exists. >> >> $ touch stuff.txt >> $ [ -a stuff.txt ]; echo $? >> 0 >> $ [ -e stuff.txt ]; echo $? >> 0 >> $ [ ! -e stuff.txt ]; echo $? >> 1 >> $ [ ! -a stuff.txt ]; echo $? >> 0 # what? > > But _this_ is an instance where -a is parsed as a binary operator (ie. > it was parsed as "\( ! \) -a \( stuff.txt \)", not "! \( -a stuff.txt > \)". -a as a binary operator is required by POSIX for XSI, therefore it > takes priority over -a as a unary operator as a bash extension. > The POSIX wording states for 3-argument tests (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html): • If $2 is a binary primary, perform the binary test of $1 and $3. • If $1 is ’!’, negate the two-argument test of $2 and $3. • If $1 is ’(’ and $3 is ’)’, perform the unary test of $2. The first two bullets both apply to your situation, but it can be assumed that the ordering of the bullets is significant. Why? Because "test \( = \)" is a case of both the first and third bullet applying, and on that case, both bash and coreutils agree that it returns false (3-argument string comparison of different strings) rather than true (parenthesized 1-argument test of non-empty =). Coreutils therefore should fix its test to favor binary -a over unary -a, when there are three arguments and the first is !. -- Eric Blake eblake redhat com+1-919-301-3266 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
Re: -a vs -e
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 9/5/13 12:45 PM, Eric Blake wrote: > The POSIX wording states for 3-argument tests > (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html): > > ? If $2 is a binary primary, perform the binary test of $1 and $3. > ? If $1 is ?!?, negate the two-argument test of $2 and $3. > ? If $1 is ?(? and $3 is ?)?, perform the unary test of $2. > > The first two bullets both apply to your situation, but it can be > assumed that the ordering of the bullets is significant. Bash assumes that the order of the bullet points in the Posix description of `test' implies prioritization. - -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/ -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (Darwin) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlIo3HMACgkQu1hp8GTqdKs9ygCfRp7ouDNHqgi0/7wGQLOOBa34 JWIAmwTKHIgeM0pCYAGP56x55dsmSo0L =xMCV -END PGP SIGNATURE-
Re: -a vs -e
On 9/5/13 1:06 AM, kneuf...@gmail.com wrote: > According to the man page, [ -a ] and [ -e ] should have the same behaviour. `test' behaves according to the number of arguments supplied. The rules are explained in the manual. The three-argument case is well defined: 3 arguments The following conditions are applied in the order listed. If the second argument is one of the binary conditional operators listed above under CONDITIONAL EXPRESSIONS, the result of the expression is the result of the binary test using the first and third arguments as operands. The -a and -o operators are considered binary operators when there are three arguments. If the first argument is !, the value is the negation of the two-argument test using the second and third arguments. If the first argument is exactly ( and the third argument is exactly ), the result is the one-argument test of the second argument. Other- wise, the expression is false. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/