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

Re: Bug with test and quoted comparisons



"brian m. carlson" wrote:
> I'm a contributor to Git and I occasionally run our testsuite in zsh's
> emulate sh mode[0].  I discovered today that one of our tests fails when
> test's comparison operator is quoted, as is demonstrated by the
> following test script:

> zsh -f -c 'emulate sh; test 2 ">" 1'
> zsh -f -c 'test 2 ">" 1'

Thanks for letting us know. With this exception, the test or \[ builtins
appear to accept all the same conditions as [[ ... ]]. Plenty of those
are newer extensions so I don't think this omission was at all related
to sh or POSIX compatibility. Perhaps it was overlooked or perhaps left
out because > and < need quoting.

Certainly, it has gone unnoticed for many years. test needs messy rules
for when something like "-a" can be treated as a string. Should =~ also
be included in the strcmp() list in par_cond_2() because I'm not sure
that's doing the right thing where the operands are -a/-o?

Oliver

diff --git a/Src/builtin.c b/Src/builtin.c
index 1c0eb331e..ba2fe1cdf 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -7243,6 +7243,10 @@ testlex(void)
 	tok = INPAR;
     else if (!strcmp(*testargs, ")"))
 	tok = OUTPAR;
+    else if (!strcmp(*testargs, "<"))
+	tok = INANG;
+    else if (!strcmp(*testargs, ">"))
+	tok = OUTANG;
     else
 	tok = STRING;
     testargs++;
diff --git a/Src/parse.c b/Src/parse.c
index 0ba7c701c..ab708a279 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -2495,6 +2495,8 @@ par_cond_2(void)
 	    /* three arguments: if the second argument is a binary operator, *
 	     * perform that binary test on the first and the third argument  */
 	    if (!strcmp(*testargs, "=")  ||
+		!strcmp(*testargs, "<") ||
+		!strcmp(*testargs, ">") ||
 		!strcmp(*testargs, "==") ||
 		!strcmp(*testargs, "!=") ||
 		(IS_DASH(**testargs) && get_cond_num(*testargs + 1) >= 0)) {
@@ -2661,6 +2663,12 @@ par_cond_triple(char *a, char *b, char *c)
 	ecstr(a);
 	ecstr(c);
 	ecadd(ecnpats++);
+    } else if ((t0 = (b[0] == '>' || b[0] == Outang) ||
+		b[0] == '<' || b[0] == Inang) && !b[1]) {
+	ecadd(WCB_COND(t0 ? COND_STRGTR : COND_STRLT, 0));
+	ecstr(a);
+	ecstr(c);
+	ecadd(ecnpats++);
     } else if ((b[0] == Equals || b[0] == '=') &&
 	       (b[1] == Equals || b[1] == '=') && !b[2]) {
 	ecadd(WCB_COND(COND_STRDEQ, 0));
diff --git a/Test/C02cond.ztst b/Test/C02cond.ztst
index daea5b4f8..3910f705f 100644
--- a/Test/C02cond.ztst
+++ b/Test/C02cond.ztst
@@ -214,6 +214,12 @@ F:scenario if you encounter it.
   [ '' != bar -a '' = '' ]
 0:strings with `[' builtin
 
+  [ zzd '>' zzb -a e '<' x ]
+0:string comparisons with `[' builtin
+
+  [ -o \> -a ]
+0:string comparison with strings that look like operators
+
   [ `echo 0` -lt `echo 1` ]
 0:substitution in `[' builtin
 




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