Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 May 1997 17:09:53 +0200 (MET DST)
From:      Zahemszky Gabor <zgabor@CoDe.hu>
To:        freebsd-questions@freebsd.org (FreeBSD questions)
Cc:        un_x@anchorage.net
Subject:   Re: tests
Message-ID:  <199705071509.RAA00810@CoDe.hu>
In-Reply-To: <Pine.BSF.3.95q.970506072104.11459A-100000@aak.anchorage.net> from Steve Howe at "May 6, 97 07:54:44 am"

next in thread | previous in thread | raw e-mail | index | archive | help
> 
> i guess this is the way "test"s work, in case
> it gives anyone new to sh programming some troubles ...
> ------------------------------------------------------------------------
> all integers are strings, but only numeric strings are "integers".
> 
> [ x ] only fails on the null string, ""(ascii 0), not on 0 (ascii 48).

Yes.

> any test, results in a 0(ascii 0) or 1(ascii 1).

No.  Any test result in a 0 status code or a non-0 status code.
-- man test
RETURN VALUES
     The test utility exits with one of the following values:

     0	     expression evaluated to true.

     1	     expression evaluated to false or expression was missing.

     >1      An error occurred.
--
> ! x is only true if x is ""(ascii 0), so it may only be used to
> 	test for the null string ("") and the result of a test([]).
> 
> therefore, -gt, -lt, -ge, -le may only be used on numeric strings,
> 	the "-" tests force a stringTOint conversion for the comparison,
> 	is my guess.

Yes, I think so.  (conversion)

> 
> and so -eq/= and -ne/!= may be used interchangably, as long as you
> 	never use an alphabetic character in a -eq/-ne comparison,
> 	since a stringTOint conversion must be done w/ -ne and -eq..
> -------------------------------------------------------------------------
> i'm "fairly" sure this is correct!

It would be better to use string compare on strings, and numeric compare on
numerals. As I've written in a previous letter:
$ test 01 -eq 1 ; echo $?
0
$ test 01 = 1 ; echo $?
1

So = and -eq are NOT interchargeable!  I think, when = returns true, than
-eq should be, too, but on the other cases, it's not true.

Bye, Gabor
--
#!/bin/ksh
Z='21N16I25C25E30, 40M30E33E25T15U!' ;IFS=' ABCDEFGHIJKLMNOPQRSTUVWXYZ ';set $Z;for i { [[ $i = ? ]]&&print $i&&break;[[ $i = ??? ]]&&j=$i&&i=${i%?};typeset -i40 i=8#$i;print -n ${i#???};[[ "$j" = ??? ]]&&print -n "${j#??} "&&j=;typeset +i i;};IFS=' 0123456789 ';set $Z;X=;for i { [[ $i = , ]]&&i=2;[[ $i = ?? ]]||typeset -l i;X="$X $i";typeset +l i;};print "$X"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199705071509.RAA00810>