tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: /bin/sh: built-in "test" command and -eq, -ne etc.



> On Sun, Aug 31, 2008 at 12:21:37PM +0300, Aleksey Cheusov wrote:
 >> Built-in test command shows the warning if arguments of -eq, -ne etc.
 >> operators are not numbers but doesn't if arguments are an empty string.
 >> 
 >>   0 ~>/bin/sh -c 'test 1 -eq 1'  
 >>   0 ~>/bin/sh -c 'test 1 -eq a' 
 >>   test: a: bad number
 >>   2 ~>/bin/sh -c 'test a -eq 1' 
 >>   test: a: bad number
 >>   2 ~>/bin/sh -c 'test "" -eq 1'
 >>   1 ~>
 >> 
 >> I propose to display a warning and make an exit status
 >> equal to 2 in the latter case too. bash and dash works this way.
 >> And I think it is useful for debugging.

> Sounds like a good idea to me.

1) bin/39446

2)

Index: test.c
===================================================================
RCS file: /pub/NetBSD-CVS/src/bin/test/test.c,v
retrieving revision 1.30
diff -u -U10 -r1.30 test.c
--- test.c      24 Sep 2006 13:24:08 -0000      1.30
+++ test.c      7 Sep 2008 11:23:52 -0000
@@ -444,21 +444,21 @@
 
        errno = 0;
        r = strtol(s, &p, 10);
 
        if (errno != 0)
              error("%s: out of range", s);
 
        while (isspace((unsigned char)*p))
              p++;
        
-       if (*p)
+       if (*p || p == s)
              error("%s: bad number", s);
 
        return (int) r;
 }
 
 static int
 newerf(const char *f1, const char *f2)
 {
        struct stat b1, b2;
 
-- 
Best regards, Aleksey Cheusov.


Home | Main Index | Thread Index | Old Index