Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: behavior of test true -a \( ! -a \)



> On 21/03/2024 10:07 GMT Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
> I know that the "test" utility (builtin in zsh) is ambiguous,
> is not completely specified by POSIX and should not be used,
> but IMHO, it should behave in a sensible and consistent way.
> 
> The following with zsh 5.9 is inconsistent:
> 
> qaa% test \( ! -a \) ; echo $?
> 1
> qaa% test true -a \( ! -a \) ; echo $?
> test: argument expected
> 2

As you can imagine, trying to put some order on the ill-defined mess
here tends to mean moving the problems around rather than fixing them.

I haven't had time to go through this completely but I think somewhere
near the root of the issue is this chunk in par_cond_2(), encountered at
the opint we get to the "!":

    if (tok == BANG) {
	/*
	 * In "test" compatibility mode, "! -a ..." and "! -o ..."
	 * are treated as "[string] [and] ..." and "[string] [or] ...".
	 */
	if (!(n_testargs > 2 && (check_cond(*testargs, "a") ||
				 check_cond(*testargs, "o"))))
	{
	    condlex();
	    ecadd(WCB_COND(COND_NOT, 0));
	    return par_cond_2();
	}
    }

in which case it needs yet more logic to decide why we shouldn't treat !
-a as a string followed by a logical "and" in this case.  To be clear,
obviously *I* can see why you want that, the question is teaching the
code without confusing it further.

pws




Messages sorted by: Reverse Date, Date, Thread, Author