9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] inequality testing in shell
@ 2015-06-14 22:54 sl
  2015-06-14 23:13 ` Neven Sajko
  0 siblings, 1 reply; 7+ messages in thread
From: sl @ 2015-06-14 22:54 UTC (permalink / raw)
  To: 9fans

     TEST(1)                                                   TEST(1)

     NAME
          test - set status according to condition

     SYNOPSIS
          test expr

     DESCRIPTION
          Test evaluates the expression expr. If the value is true the
          exit status is null; otherwise the exit status is non-null.
          If there are no arguments the exit status is non-null.

          The following primitives are used to construct expr.

          -r file    True if the file exists (is accessible) and is
                     readable.
          -w file    True if the file exists and is writable.
          -x file    True if the file exists and has execute permis-
                     sion.
          -e file    True if the file exists.
          -f file    True if the file exists and is a plain file.
          -d file    True if the file exists and is a directory.
          -s file    True if the file exists and has a size greater
                     than zero.
          -t fildes  True if the open file whose file descriptor num-
                     ber is fildes (1 by default) is the same file as
                     /dev/cons.
          -A file    True if the file exists and is append-only.
          -L file    True if the file exists and is exclusive-use.
          -Tfile     True if the file exists and is temporary.
          s1 = s2    True if the strings s1 and s2 are identical.
          s1 != s2   True if the strings s1 and s2 are not identical.
          s1         True if s1 is not the null string.  (Deprecated.)
          -n s1      True if the length of string s1 is non-zero.
          -z s1      True if the length of string s1 is zero.
          n1 -eq n2  True if the integers n1 and n2 are arithmetically
                     equal.  Any of the comparisons -ne, -gt, -ge,
                     -lt, or -le may be used in place of -eq.  The
                     (nonstandard) construct -l string, meaning the
                     length of string, may be used in place of an
                     integer.
          a -nt b    True if file a is newer than (modified after)
                     file b.
          a -ot b    True if file a is older than (modified before)
                     file b.
          f -older t True if file f is older than (modified before)
                     time t. If t is a integer followed by the letters
                     y(years), M(months), d(days), h(hours),
                     m(minutes), or s(seconds), it represents current
                     time minus the specified time.  If there is no
                     letter, it represents seconds since epoch.  You
                     can also concatenate mixed units.  For example,
                     3d12h means three days and twelve hours ago.

          These primaries may be combined with the following opera-
          tors:

          !         unary negation operator
          -o        binary or operator
          -a        binary and operator; higher precedence than -o
          ( expr )  parentheses for grouping.

          The primitives -b, -u, -g, and -s return false; they are
          recognized for compatibility with POSIX.

          Notice that all the operators and flags are separate argu-
          ments to test. Notice also that parentheses and equal signs
          are meaningful to rc and must be enclosed in quotes.

     EXAMPLES
          Test is a dubious way to check for specific character
          strings: it uses a process to do what an rc(1) match or
          switch statement can do.  The first example is not only
          inefficient but wrong, because test understands the pur-
          ported string "-c" as an option.

               if (test $1 '=' "-c") echo OK # wrong!

          A better way is

               if (~ $1 -c) echo OK

          Test whether `abc' is in the current directory.

               test -f abc -o -d abc

     SOURCE
          /sys/src/cmd/test.c

     SEE ALSO
          rc(1)

     BUGS
          Won't complain about extraneous arguments since there may be
          arguments left unprocessed by short-circuit evaluation of -a
          or -o.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] inequality testing in shell
  2015-06-14 22:54 [9fans] inequality testing in shell sl
@ 2015-06-14 23:13 ` Neven Sajko
  2015-06-14 23:30   ` Steve Simon
  0 siblings, 1 reply; 7+ messages in thread
From: Neven Sajko @ 2015-06-14 23:13 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> Any of the comparisons -ne, -gt, -ge, -lt, or -le may be used in place of -eq.

Oops :)
Sorry, I didn't notice that there under the -eq entry.

Thanks



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] inequality testing in shell
  2015-06-14 23:13 ` Neven Sajko
@ 2015-06-14 23:30   ` Steve Simon
  0 siblings, 0 replies; 7+ messages in thread
From: Steve Simon @ 2015-06-14 23:30 UTC (permalink / raw)
  To: 9fans

> Oops :)
> Sorry, I didn't notice that there under the -eq entry.

ditto :-(

As I said, never used em.

-Steve



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] inequality testing in shell
  2015-06-14 21:08 Neven Sajko
  2015-06-14 21:28 ` Kurt H Maier
  2015-06-14 22:11 ` Steve Simon
@ 2015-06-15 13:14 ` erik quanstrom
  2 siblings, 0 replies; 7+ messages in thread
From: erik quanstrom @ 2015-06-15 13:14 UTC (permalink / raw)
  To: 9fans

On Sun Jun 14 14:12:13 PDT 2015, nsajko@gmail.com wrote:
> Why doesn't the plan9 test program have features for checking
> inequalities (i.e. greater than etc.)?
> What do you guys do when you want to test for inequality?
>
> Maybe the most convenient way to do that in the shell would be if I
> made two tiny programs, lt & gt which would check their two arguments.

to be more pedantic than necessary, test is not part of the plan 9 shell.
the ~ operator is the way to test for (string) equality or inequality.
! is shell negation, so for non-negative integral x,

~ $x [1-3]	-> x < 4
! ~ $x [1-3]	-> x >= 4

the shell doesn't understand numbers.

outside the shell, hoc or awk are alternatives to test

- erik



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] inequality testing in shell
  2015-06-14 21:08 Neven Sajko
  2015-06-14 21:28 ` Kurt H Maier
@ 2015-06-14 22:11 ` Steve Simon
  2015-06-15 13:14 ` erik quanstrom
  2 siblings, 0 replies; 7+ messages in thread
From: Steve Simon @ 2015-06-14 22:11 UTC (permalink / raw)
  To: 9fans

It would be easy to extend test to add these features,
you could submit a patch if you feel strongly.

To be honest I have never had the need for inequalaties,
perhaps I have been lucky.

-Steve



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] inequality testing in shell
  2015-06-14 21:08 Neven Sajko
@ 2015-06-14 21:28 ` Kurt H Maier
  2015-06-14 22:11 ` Steve Simon
  2015-06-15 13:14 ` erik quanstrom
  2 siblings, 0 replies; 7+ messages in thread
From: Kurt H Maier @ 2015-06-14 21:28 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Quoting Neven Sajko <nsajko@gmail.com>:

> Why doesn't the plan9 test program have features for checking
> inequalities (i.e. greater than etc.)?
> What do you guys do when you want to test for inequality?

I use test(1) which does, in fact, support these features, as
described in the man page.

khm




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [9fans]  inequality testing in shell
@ 2015-06-14 21:08 Neven Sajko
  2015-06-14 21:28 ` Kurt H Maier
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Neven Sajko @ 2015-06-14 21:08 UTC (permalink / raw)
  To: 9fans

Why doesn't the plan9 test program have features for checking
inequalities (i.e. greater than etc.)?
What do you guys do when you want to test for inequality?

Maybe the most convenient way to do that in the shell would be if I
made two tiny programs, lt & gt which would check their two arguments.



Regards,
Neven Sajko



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-06-15 13:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-14 22:54 [9fans] inequality testing in shell sl
2015-06-14 23:13 ` Neven Sajko
2015-06-14 23:30   ` Steve Simon
  -- strict thread matches above, loose matches on Subject: below --
2015-06-14 21:08 Neven Sajko
2015-06-14 21:28 ` Kurt H Maier
2015-06-14 22:11 ` Steve Simon
2015-06-15 13:14 ` erik quanstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).