2005-01-10 08:39:26 +00:00
|
|
|
.\"-
|
1994-05-26 06:18:55 +00:00
|
|
|
.\" Copyright (c) 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to Berkeley by
|
|
|
|
.\" the Institute of Electrical and Electronics Engineers, Inc.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" @(#)test.1 8.1 (Berkeley) 5/31/93
|
1999-08-27 23:15:48 +00:00
|
|
|
.\" $FreeBSD$
|
1994-05-26 06:18:55 +00:00
|
|
|
.\"
|
2016-10-05 19:47:02 +00:00
|
|
|
.Dd October 5, 2016
|
1994-05-26 06:18:55 +00:00
|
|
|
.Dt TEST 1
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
1999-08-16 09:44:09 +00:00
|
|
|
.Nm test ,
|
|
|
|
.Nm \&[
|
1994-05-26 06:18:55 +00:00
|
|
|
.Nd condition evaluation utility
|
|
|
|
.Sh SYNOPSIS
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
1994-05-26 06:18:55 +00:00
|
|
|
.Ar expression
|
2000-11-18 14:44:30 +00:00
|
|
|
.Nm \&[
|
2012-05-13 14:16:04 +00:00
|
|
|
.Ar expression Cm \&]
|
1994-05-26 06:18:55 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
1994-05-26 06:18:55 +00:00
|
|
|
utility evaluates the expression and, if it evaluates
|
|
|
|
to true, returns a zero (true) exit status; otherwise
|
|
|
|
it returns 1 (false).
|
2006-12-14 10:42:46 +00:00
|
|
|
If there is no expression,
|
|
|
|
.Nm
|
|
|
|
also
|
1994-05-26 06:18:55 +00:00
|
|
|
returns 1 (false).
|
|
|
|
.Pp
|
|
|
|
All operators and flags are separate arguments to the
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
1994-05-26 06:18:55 +00:00
|
|
|
utility.
|
|
|
|
.Pp
|
|
|
|
The following primaries are used to construct expression:
|
1999-08-16 09:44:09 +00:00
|
|
|
.Bl -tag -width Ar
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Fl b Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a block special
|
|
|
|
file.
|
|
|
|
.It Fl c Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a character
|
|
|
|
special file.
|
|
|
|
.It Fl d Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a directory.
|
|
|
|
.It Fl e Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists (regardless of type).
|
|
|
|
.It Fl f Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a regular file.
|
|
|
|
.It Fl g Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and its set group ID flag
|
|
|
|
is set.
|
|
|
|
.It Fl h Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a symbolic link.
|
1999-08-16 09:44:09 +00:00
|
|
|
This operator is retained for compatibility with previous versions of
|
2000-03-01 10:43:09 +00:00
|
|
|
this program.
|
|
|
|
Do not rely on its existence; use
|
1999-08-16 09:44:09 +00:00
|
|
|
.Fl L
|
|
|
|
instead.
|
|
|
|
.It Fl k Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and its sticky bit is set.
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Fl n Ar string
|
|
|
|
True if the length of
|
|
|
|
.Ar string
|
|
|
|
is nonzero.
|
|
|
|
.It Fl p Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
is a named pipe
|
2001-08-07 15:48:51 +00:00
|
|
|
.Pq Tn FIFO .
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Fl r Ar file
|
|
|
|
True if
|
2001-07-15 07:53:42 +00:00
|
|
|
.Ar file
|
1997-06-02 06:40:08 +00:00
|
|
|
exists and is readable.
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Fl s Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and has a size greater
|
|
|
|
than zero.
|
2001-08-31 15:38:53 +00:00
|
|
|
.It Fl t Ar file_descriptor
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the file whose file descriptor number
|
|
|
|
is
|
|
|
|
.Ar file_descriptor
|
1999-08-16 09:44:09 +00:00
|
|
|
is open and is associated with a terminal.
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Fl u Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and its set user ID flag
|
|
|
|
is set.
|
|
|
|
.It Fl w Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is writable.
|
|
|
|
True
|
|
|
|
indicates only that the write flag is on.
|
|
|
|
The file is not writable on a read-only file
|
|
|
|
system even if this test indicates true.
|
|
|
|
.It Fl x Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is executable.
|
|
|
|
True
|
|
|
|
indicates only that the execute flag is on.
|
|
|
|
If
|
|
|
|
.Ar file
|
|
|
|
is a directory, true indicates that
|
|
|
|
.Ar file
|
|
|
|
can be searched.
|
|
|
|
.It Fl z Ar string
|
|
|
|
True if the length of
|
|
|
|
.Ar string
|
|
|
|
is zero.
|
1999-08-16 09:44:09 +00:00
|
|
|
.It Fl L Ar file
|
2001-07-15 07:53:42 +00:00
|
|
|
True if
|
1999-08-16 09:44:09 +00:00
|
|
|
.Ar file
|
|
|
|
exists and is a symbolic link.
|
|
|
|
.It Fl O Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and its owner matches the effective user id of this process.
|
|
|
|
.It Fl G Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and its group matches the effective group id of this process.
|
|
|
|
.It Fl S Ar file
|
|
|
|
True if
|
|
|
|
.Ar file
|
|
|
|
exists and is a socket.
|
|
|
|
.It Ar file1 Fl nt Ar file2
|
2013-05-31 22:54:20 +00:00
|
|
|
True if
|
1999-08-16 09:44:09 +00:00
|
|
|
.Ar file1
|
2013-05-31 22:54:20 +00:00
|
|
|
exists and is newer than
|
1999-08-16 09:44:09 +00:00
|
|
|
.Ar file2 .
|
|
|
|
.It Ar file1 Fl ot Ar file2
|
2013-05-31 22:54:20 +00:00
|
|
|
True if
|
2013-02-25 19:05:40 +00:00
|
|
|
.Ar file1
|
2013-05-31 22:54:20 +00:00
|
|
|
exists and is older than
|
1999-08-16 09:44:09 +00:00
|
|
|
.Ar file2 .
|
|
|
|
.It Ar file1 Fl ef Ar file2
|
|
|
|
True if
|
|
|
|
.Ar file1
|
|
|
|
and
|
|
|
|
.Ar file2
|
|
|
|
exist and refer to the same file.
|
1994-05-26 06:18:55 +00:00
|
|
|
.It Ar string
|
|
|
|
True if
|
|
|
|
.Ar string
|
|
|
|
is not the null
|
|
|
|
string.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar s1 Cm = Ar s2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the strings
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s1
|
1994-05-26 06:18:55 +00:00
|
|
|
and
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s2
|
1994-05-26 06:18:55 +00:00
|
|
|
are identical.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar s1 Cm != Ar s2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the strings
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s1
|
1994-05-26 06:18:55 +00:00
|
|
|
and
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s2
|
1994-05-26 06:18:55 +00:00
|
|
|
are not identical.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar s1 Cm < Ar s2
|
1999-08-16 09:44:09 +00:00
|
|
|
True if string
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s1
|
1999-08-16 09:44:09 +00:00
|
|
|
comes before
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s2
|
2004-07-03 01:49:17 +00:00
|
|
|
based on the binary value of their characters.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar s1 Cm > Ar s2
|
1999-08-16 09:44:09 +00:00
|
|
|
True if string
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s1
|
1999-08-16 09:44:09 +00:00
|
|
|
comes after
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar s2
|
2004-07-03 01:49:17 +00:00
|
|
|
based on the binary value of their characters.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar n1 Fl eq Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integers
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
and
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
are algebraically
|
|
|
|
equal.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar n1 Fl ne Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integers
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
and
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
are not
|
|
|
|
algebraically equal.
|
2006-12-14 10:42:46 +00:00
|
|
|
.It Ar n1 Fl gt Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
is algebraically
|
|
|
|
greater than the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2 .
|
|
|
|
.It Ar n1 Fl ge Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
is algebraically
|
|
|
|
greater than or equal to the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2 .
|
|
|
|
.It Ar n1 Fl lt Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
is algebraically less
|
|
|
|
than the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2 .
|
|
|
|
.It Ar n1 Fl le Ar n2
|
1994-05-26 06:18:55 +00:00
|
|
|
True if the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n1
|
1994-05-26 06:18:55 +00:00
|
|
|
is algebraically less
|
|
|
|
than or equal to the integer
|
2006-12-14 10:42:46 +00:00
|
|
|
.Ar n2 .
|
1994-05-26 06:18:55 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2004-06-07 12:42:38 +00:00
|
|
|
If
|
|
|
|
.Ar file
|
|
|
|
is a symbolic link,
|
|
|
|
.Nm
|
|
|
|
will fully dereference it and then evaluate the expression
|
|
|
|
against the file referenced, except for the
|
|
|
|
.Fl h
|
|
|
|
and
|
|
|
|
.Fl L
|
|
|
|
primaries.
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
These primaries can be combined with the following operators:
|
|
|
|
.Bl -tag -width Ar
|
|
|
|
.It Cm \&! Ar expression
|
|
|
|
True if
|
|
|
|
.Ar expression
|
|
|
|
is false.
|
|
|
|
.It Ar expression1 Fl a Ar expression2
|
|
|
|
True if both
|
|
|
|
.Ar expression1
|
|
|
|
and
|
|
|
|
.Ar expression2
|
|
|
|
are true.
|
|
|
|
.It Ar expression1 Fl o Ar expression2
|
|
|
|
True if either
|
|
|
|
.Ar expression1
|
|
|
|
or
|
|
|
|
.Ar expression2
|
|
|
|
are true.
|
2010-09-10 14:03:58 +00:00
|
|
|
.It Cm \&( Ar expression Cm \&)
|
1994-05-26 06:18:55 +00:00
|
|
|
True if expression is true.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fl a
|
|
|
|
operator has higher precedence than the
|
|
|
|
.Fl o
|
|
|
|
operator.
|
2001-11-17 19:10:11 +00:00
|
|
|
.Pp
|
|
|
|
Some shells may provide a builtin
|
|
|
|
.Nm
|
|
|
|
command which is similar or identical to this utility.
|
|
|
|
Consult the
|
|
|
|
.Xr builtin 1
|
|
|
|
manual page.
|
1994-05-26 06:18:55 +00:00
|
|
|
.Sh GRAMMAR AMBIGUITY
|
2001-07-15 07:53:42 +00:00
|
|
|
The
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
2004-07-02 21:04:19 +00:00
|
|
|
grammar is inherently ambiguous.
|
2010-09-11 10:49:56 +00:00
|
|
|
In order to assure a degree of consistency,
|
2001-07-15 07:53:42 +00:00
|
|
|
the cases described in the
|
|
|
|
.St -p1003.2 ,
|
1994-05-26 06:18:55 +00:00
|
|
|
section D11.2/4.62.4, standard
|
|
|
|
are evaluated consistently according to the rules specified in the
|
2004-07-02 21:04:19 +00:00
|
|
|
standards document.
|
|
|
|
All other cases are subject to the ambiguity in the
|
1994-05-26 06:18:55 +00:00
|
|
|
command semantics.
|
2010-09-10 14:00:27 +00:00
|
|
|
.Pp
|
|
|
|
In particular, only expressions containing
|
|
|
|
.Fl a ,
|
|
|
|
.Fl o ,
|
|
|
|
.Cm \&(
|
|
|
|
or
|
|
|
|
.Cm \&)
|
|
|
|
can be ambiguous.
|
2009-01-07 01:03:23 +00:00
|
|
|
.Sh EXIT STATUS
|
1994-05-26 06:18:55 +00:00
|
|
|
The
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
1994-05-26 06:18:55 +00:00
|
|
|
utility exits with one of the following values:
|
2006-12-14 10:42:46 +00:00
|
|
|
.Bl -tag -width indent
|
1994-05-26 06:18:55 +00:00
|
|
|
.It 0
|
|
|
|
expression evaluated to true.
|
|
|
|
.It 1
|
|
|
|
expression evaluated to false or expression was
|
|
|
|
missing.
|
|
|
|
.It >1
|
|
|
|
An error occurred.
|
|
|
|
.El
|
2013-05-31 22:57:04 +00:00
|
|
|
.Sh EXAMPLES
|
|
|
|
Implement
|
|
|
|
.Li test FILE1 -nt FILE2
|
|
|
|
using only
|
|
|
|
.Tn POSIX
|
|
|
|
functionality:
|
|
|
|
.Pp
|
|
|
|
.Dl test -n \&"$(find -L -- FILE1 -prune -newer FILE2 2>/dev/null)\&"
|
|
|
|
.Pp
|
|
|
|
This can be modified using non-standard
|
|
|
|
.Xr find 1
|
|
|
|
primaries like
|
|
|
|
.Cm -newerca
|
|
|
|
to compare other timestamps.
|
2012-12-27 13:21:37 +00:00
|
|
|
.Sh COMPATIBILITY
|
|
|
|
For compatibility with some other implementations,
|
|
|
|
the
|
|
|
|
.Cm =
|
|
|
|
primary can be substituted with
|
|
|
|
.Cm ==
|
|
|
|
with the same meaning.
|
1998-12-18 03:16:47 +00:00
|
|
|
.Sh SEE ALSO
|
2001-11-17 19:10:11 +00:00
|
|
|
.Xr builtin 1 ,
|
1998-12-18 03:16:47 +00:00
|
|
|
.Xr expr 1 ,
|
2013-05-31 22:57:04 +00:00
|
|
|
.Xr find 1 ,
|
2004-06-07 12:42:38 +00:00
|
|
|
.Xr sh 1 ,
|
2013-05-31 22:57:04 +00:00
|
|
|
.Xr stat 1 ,
|
2004-06-07 12:42:38 +00:00
|
|
|
.Xr symlink 7
|
1994-05-26 06:18:55 +00:00
|
|
|
.Sh STANDARDS
|
|
|
|
The
|
2000-11-20 11:39:41 +00:00
|
|
|
.Nm
|
1999-08-16 09:44:09 +00:00
|
|
|
utility implements a superset of the
|
1994-05-26 06:18:55 +00:00
|
|
|
.St -p1003.2
|
1999-08-16 09:44:09 +00:00
|
|
|
specification.
|
2013-05-31 22:55:21 +00:00
|
|
|
The primaries
|
|
|
|
.Cm < ,
|
|
|
|
.Cm == ,
|
|
|
|
.Cm > ,
|
|
|
|
.Fl ef ,
|
|
|
|
.Fl nt ,
|
|
|
|
.Fl ot ,
|
|
|
|
.Fl G ,
|
|
|
|
and
|
|
|
|
.Fl O
|
|
|
|
are extensions.
|
2016-10-05 19:47:02 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
A
|
|
|
|
.Nm
|
|
|
|
utility appeared in
|
|
|
|
.At v7 .
|
2006-07-27 19:08:21 +00:00
|
|
|
.Sh BUGS
|
|
|
|
Both sides are always evaluated in
|
|
|
|
.Fl a
|
|
|
|
and
|
2010-09-10 14:00:27 +00:00
|
|
|
.Fl o .
|
2006-07-31 09:25:20 +00:00
|
|
|
For instance, the writable status of
|
|
|
|
.Pa file
|
|
|
|
will be tested by the following command even though the former expression
|
|
|
|
indicated false, which results in a gratuitous access to the file system:
|
|
|
|
.Dl "[ -z abc -a -w file ]"
|
2010-09-10 14:00:27 +00:00
|
|
|
To avoid this, write
|
|
|
|
.Dl "[ -z abc ] && [ -w file ]"
|