Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought


test                         Command                         test




Evaluate conditional expression

test expression ...

test  evaluates  an expression,  which  consists  of string  com-
parisons,  numerical comparisons, and  tests of  file attributes.
For example, a test command  might be used within a shell command
file to test whether a  certain file exists and is readable.  The
logical result  (true or false) of the  expression is returned by
the command, for use by a shell construct such as if.

expression is constructed  from the following elements, which are
true if the given condition holds and false if not:

-d file   file exists and is a directory.

-f file   file exists and is not a directory.

-n string string has nonzero length.

-r file   file exists and is readable.

-s file   file exists and has nonzero size.

-t [fd]    fd is the  file descriptor number  of a file  which is
          open and a terminal.  If no fd is given, it defaults to
          the standard output (file descriptor 1).

-w file   file exists and is writable.

-z string string has zero length (is a null string).

string    string has nonzero length.

s1 = s2   String s1 is equal to string s2.

s1 != s2  String s1 is not equal to string s2.

n1 -eq n2 Numbers n1 and n2 are equal.

n1 -ne n2 Numbers n1 and n2 are not equal.

n1 -gt n2 Number n1 is greater than n2.

n1 -ge n2 Number n1 is greater than or equal to n2.

n1 -lt n2 Number n1 is less than n2.

n1 -le n2 Number n1 is less than or equal to n2.

! exp     Negates the logical value of expression exp.

exp1 -a exp2
          Both expressions exp1 and exp2 are true.


COHERENT Lexicon                                           Page 1



test                         Command                         test




exp1 -o exp2
          Either expression exp1 or exp2 is true.  -a has greater
          precedence than -o.

(exp)     Parentheses allow expression grouping.

***** Example *****

The following example  uses the test command to determine whether
a file is writable.


if test ! -w /dev/lp
then
        echo The line printer is inaccessible.
fi


Under COHERENT, the command `[' is linked to test.  If invoked as
`[', test  checks that its last argument is  `]'.  This allows an
alternative syntax: simply enclose expression in square brackets.
For example, the above example can be written as follows:


if [ ! -w /dev/lp ]
then
        echo The line printer is inaccessible.
fi


For a more extended example of the square-bracket syntax, see sh.

***** See Also *****

commands, expr, find, if, sh, while





















COHERENT Lexicon                                           Page 2


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026