COHERENT manpages

This page displays the COHERENT manpage for test [Evaluate conditional expression].

List of available manpages
Index


test -- Command

Evaluate conditional expression
test expression ...

test  evaluates  an  expression,  which  consists  of  string  comparisons,
numerical comparisons,  and tests of file attributes.   For example, a test
command might be used within a  shell script 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 another shell construct, such as the command if.

Under  the Korn  shell, test  is a  built-in command  that returns  zero if
expression is true,  and one if it is false.   Under the Bourne shell, test
is  not a  built-in  command; rather,  the  Bourne shell  uses the  command
/bin/test to  test expressions.  /bin/sh returns zero  if the expression is
true,  one if  it is  false, and  two if  a syntax  error (or  other error)
occurred.

Expression Options

test recognizes  the following options, one  or more of which  can be built
into an expression:

! exp       Negates the logical value of expression exp.
string1 != string2
            string1 is not equal to string2.
string1 < string2
            string1 is lexicographically less than string2 (sh only).
string1 = string2
            string1 is equal to string2.
string1 > string2
            string1 is lexicographically greater than string2 (sh only).
(exp)       Parentheses allow expression grouping.
exp1 -a exp2
            Both expressions exp1 and exp2 are true.
-b file     file is a block-special device.
-c file     file is a character-special file.
-d file     file exists and is a directory.
-e file     file exists (/bin/test only).
file1 -ef file2
            file1 is the same file as file2.
n1 -eq n2   Numbers n1  and n2 are  equal.  Please note  that test evalutes
            the expression   as zero.  Thus,  if one of the  arguments is a
            variable that  is not set, test  treats it as if  it were zero.
            For example, consider the expression:

                if [ "$notset" -eq 0 ]
            If notset is not set, test  evaluates it to zero and so returns
            true.
-f file     file exists and is an ordinary file.
-g file     File mode has setgid bit.
n1 -ge n2   Number n1 is greater than or equal to n2.
n1 -gt n2   Number n1 is greater than n2.
-k file     File mode has sticky bit.
-L file     File is a symbolic link.
n1 -le n2   Number n1 is less than or equal to n2.
n1 -lt n2   Number n1 is less than n2.
-n string   string has nonzero length.
n1 -ne n2   Numbers n1 and n2 are not equal.
file1 -nt file2
            file1 is newer than file2.
exp1 -o exp2
            Either  expression  exp1  or  exp2  is  true.  -a  has  greater
            precedence than -o.
file1 -ot file2
            file1 is older than file2.
-p file     file is a named pipe.
-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  that is open and a
            terminal.  The  Bourne shell requires  that fd be  given; under
            the Korn shell,  however, defaults to the standard output (file
            descriptor 1) if no fd is given.
-u file     File mode has setuid set.
-w file     file exists and is writable.
-x file     file exists and executable.
-z string   string has zero length (is a null string).
string      string has nonzero length.

Implementations of test

The implementation  of test under  the Bourne shell has  been rewritten for
COHERENT release 4.2,  both to extend its range of  features and to make it
more  compliant with  published standards.  Although  this makes  test more
useful to  programmers, it may  create problems when  you try to  execute a
Bourne-shell  script  written  under COHERENT  release  4.2  on an  earlier
release  of  COHERENT.   The following  describes  how  the Bourne  shell's
implementation of  test was designed; and how it  differs both from earlier
implementations under  the Bourne shell  and from the  implementation under
the Korn shell.

To begin, the Bourne shell's implementation of test attempts to comply with
the POSIX  Standard, comply  with previous  COHERENT releases of  test, and
comply with System-V UNIX  to the greatest extent possible.  However, these
objectives are  mutually exclusive.   See the POSIX  Standard P1003.2/D11.2
§4.62, especially the Rationale,  for details of some of the problems.
In particular,  System V  and Berkeley  differ in the  way they  parse some
expressions, which leads the POSIX  Standard to specify test behavior for a
minimal set of expressions, not including -a and -o.

The  following details  differences  among the  various implementations  of
test.  First, the  following  options were  not implemented  in the  Bourne
shell's  implementation of  test prior  to COHERENT  release 4.2,  but were
included in the Korn shell's implementation:  -b, -c, -ef, -g, -k, -L, -nt,
-ot, -p, -u, and -x. Of these, the following are not described in the POSIX
Standard: -k,  -L, -ef, -nt,  and -ot. Note that  Bourne-shell scripts that
use any of  the above options to test will  not run on versions of COHERENT
prior to release 4.2, but will run under the Korn shell.

Next, the  Bourne shell  for COHERENT  4.2 implements the  POSIX Standard's
option -e and the options <  and >. Bourne-shell scripts that use any
of these three  options to test will not run  on versions of COHERENT prior
to release 4.2, nor will they run under the Korn shell.

The  definitions of  the  options -f  and  -t have  been  changed from  the
Berkeley  standard  to  that described  in  the  POSIX Standard.   Berkeley
defines -f  as meaning that a  file exists and is  not a directory; whereas
the  POSIX Standard  defines it  as  meaning that  a file  exists and  is a
regular file.  Versions  of the Bourne shell prior to  COHERENT 4.2 use the
Berkeley  definitions; whereas  all version  of the  Korn shell  and Bourne
shell  under COHERENT  4.2 use the  POSIX Standard's  definition.  Berkeley
gives -t a default value of one if it is not used with an argument; whereas
the POSIX Standard  requires that -t have an argument.   The Korn shell and
all versions  of the Bourne  shell prior to  COHERENT 4.2 use  the Berkeley
definition;  whereas the  Bourne shell  under COHERENT  4.2 uses  the POSIX
Standard's definition.  These differences are subtle, but important.  Thus,
a  Bourne shell  script  that uses  either  of these  options  may not  run
correctly  when  imported  into  COHERENT  4.2  from  earlier  versions  of
COHERENT, or when exported from COHERENT 4.2 to them or to the Korn shell.

Finally, test  under the  Korn shell  and under the  Bourne shell  prior to
COHERENT 4.2  returns zero if an  expression is true and  one either if the
expression  is  false  or  if  the  expression contained  a  syntax  error.
However, test  under the Bourne shell  for COHERENT 4.2 returns  zero if an
expression is true, one if it is false, and two if a syntax error occurred.
Bourne-shell scripts that pay close  attention to what test returns may not
run correctly when  imported into COHERENT 4.2 from earlier implementations
of  COHERENT, or  when exported  from COHERENT 4.2  to earlier  versions of
COHERENT or to the Korn shell.

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,
ksh,
sh,
while

Notes

The  Korn shell's  version of  this command is  based on  the public-domain
version written by Erik Baalbergen and Arnold Robbins.