xref: /freebsd/bin/test/test.1 (revision 7eb5016ab559260d825044e25ec4267dee4b49d1)
19ddb49cbSWarner Losh.\"-
24b88c807SRodney W. Grimes.\" Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes.\"
54b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes.\" the Institute of Electrical and Electronics Engineers, Inc.
74b88c807SRodney W. Grimes.\"
84b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes.\" are met:
114b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes.\"    without specific prior written permission.
194b88c807SRodney W. Grimes.\"
204b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes.\" SUCH DAMAGE.
314b88c807SRodney W. Grimes.\"
324b88c807SRodney W. Grimes.\"     @(#)test.1	8.1 (Berkeley) 5/31/93
332a456239SPeter Wemm.\" $FreeBSD$
344b88c807SRodney W. Grimes.\"
356a215886SYaroslav Tykhiy.Dd July 31, 2006
364b88c807SRodney W. Grimes.Dt TEST 1
374b88c807SRodney W. Grimes.Os
384b88c807SRodney W. Grimes.Sh NAME
39717a08b6SSheldon Hearn.Nm test ,
40717a08b6SSheldon Hearn.Nm \&[
414b88c807SRodney W. Grimes.Nd condition evaluation utility
424b88c807SRodney W. Grimes.Sh SYNOPSIS
43be8b1497SRuslan Ermilov.Nm
444b88c807SRodney W. Grimes.Ar expression
45238fe5f7SRuslan Ermilov.Nm \&[
46717a08b6SSheldon Hearn.Ar expression Cm ]
474b88c807SRodney W. Grimes.Sh DESCRIPTION
484b88c807SRodney W. GrimesThe
49be8b1497SRuslan Ermilov.Nm
504b88c807SRodney W. Grimesutility evaluates the expression and, if it evaluates
514b88c807SRodney W. Grimesto true, returns a zero (true) exit status; otherwise
524b88c807SRodney W. Grimesit returns 1 (false).
537eb5016aSRuslan ErmilovIf there is no expression,
547eb5016aSRuslan Ermilov.Nm
557eb5016aSRuslan Ermilovalso
564b88c807SRodney W. Grimesreturns 1 (false).
574b88c807SRodney W. Grimes.Pp
584b88c807SRodney W. GrimesAll operators and flags are separate arguments to the
59be8b1497SRuslan Ermilov.Nm
604b88c807SRodney W. Grimesutility.
614b88c807SRodney W. Grimes.Pp
624b88c807SRodney W. GrimesThe following primaries are used to construct expression:
63717a08b6SSheldon Hearn.Bl -tag -width Ar
644b88c807SRodney W. Grimes.It Fl b Ar file
654b88c807SRodney W. GrimesTrue if
664b88c807SRodney W. Grimes.Ar file
674b88c807SRodney W. Grimesexists and is a block special
684b88c807SRodney W. Grimesfile.
694b88c807SRodney W. Grimes.It Fl c Ar file
704b88c807SRodney W. GrimesTrue if
714b88c807SRodney W. Grimes.Ar file
724b88c807SRodney W. Grimesexists and is a character
734b88c807SRodney W. Grimesspecial file.
744b88c807SRodney W. Grimes.It Fl d Ar file
754b88c807SRodney W. GrimesTrue if
764b88c807SRodney W. Grimes.Ar file
774b88c807SRodney W. Grimesexists and is a directory.
784b88c807SRodney W. Grimes.It Fl e Ar file
794b88c807SRodney W. GrimesTrue if
804b88c807SRodney W. Grimes.Ar file
814b88c807SRodney W. Grimesexists (regardless of type).
824b88c807SRodney W. Grimes.It Fl f Ar file
834b88c807SRodney W. GrimesTrue if
844b88c807SRodney W. Grimes.Ar file
854b88c807SRodney W. Grimesexists and is a regular file.
864b88c807SRodney W. Grimes.It Fl g Ar file
874b88c807SRodney W. GrimesTrue if
884b88c807SRodney W. Grimes.Ar file
894b88c807SRodney W. Grimesexists and its set group ID flag
904b88c807SRodney W. Grimesis set.
914b88c807SRodney W. Grimes.It Fl h Ar file
924b88c807SRodney W. GrimesTrue if
934b88c807SRodney W. Grimes.Ar file
944b88c807SRodney W. Grimesexists and is a symbolic link.
95717a08b6SSheldon HearnThis operator is retained for compatibility with previous versions of
96bef84d6bSSheldon Hearnthis program.
97bef84d6bSSheldon HearnDo not rely on its existence; use
98717a08b6SSheldon Hearn.Fl L
99717a08b6SSheldon Hearninstead.
100717a08b6SSheldon Hearn.It Fl k Ar file
101717a08b6SSheldon HearnTrue if
102717a08b6SSheldon Hearn.Ar file
103717a08b6SSheldon Hearnexists and its sticky bit is set.
1044b88c807SRodney W. Grimes.It Fl n Ar string
1054b88c807SRodney W. GrimesTrue if the length of
1064b88c807SRodney W. Grimes.Ar string
1074b88c807SRodney W. Grimesis nonzero.
1084b88c807SRodney W. Grimes.It Fl p Ar file
1094b88c807SRodney W. GrimesTrue if
1104b88c807SRodney W. Grimes.Ar file
1114b88c807SRodney W. Grimesis a named pipe
112c4d9468eSRuslan Ermilov.Pq Tn FIFO .
1134b88c807SRodney W. Grimes.It Fl r Ar file
1144b88c807SRodney W. GrimesTrue if
115c263cc21SPhilippe Charnier.Ar file
116c263cc21SPhilippe Charnierexists and is readable.
1174b88c807SRodney W. Grimes.It Fl s Ar file
1184b88c807SRodney W. GrimesTrue if
1194b88c807SRodney W. Grimes.Ar file
1204b88c807SRodney W. Grimesexists and has a size greater
1214b88c807SRodney W. Grimesthan zero.
12246c128b5SDima Dorfman.It Fl t Ar file_descriptor
1234b88c807SRodney W. GrimesTrue if the file whose file descriptor number
1244b88c807SRodney W. Grimesis
1254b88c807SRodney W. Grimes.Ar file_descriptor
126717a08b6SSheldon Hearnis open and is associated with a terminal.
1274b88c807SRodney W. Grimes.It Fl u Ar file
1284b88c807SRodney W. GrimesTrue if
1294b88c807SRodney W. Grimes.Ar file
1304b88c807SRodney W. Grimesexists and its set user ID flag
1314b88c807SRodney W. Grimesis set.
1324b88c807SRodney W. Grimes.It Fl w Ar file
1334b88c807SRodney W. GrimesTrue if
1344b88c807SRodney W. Grimes.Ar file
1354b88c807SRodney W. Grimesexists and is writable.
1364b88c807SRodney W. GrimesTrue
1374b88c807SRodney W. Grimesindicates only that the write flag is on.
1384b88c807SRodney W. GrimesThe file is not writable on a read-only file
1394b88c807SRodney W. Grimessystem even if this test indicates true.
1404b88c807SRodney W. Grimes.It Fl x Ar file
1414b88c807SRodney W. GrimesTrue if
1424b88c807SRodney W. Grimes.Ar file
1434b88c807SRodney W. Grimesexists and is executable.
1444b88c807SRodney W. GrimesTrue
1454b88c807SRodney W. Grimesindicates only that the execute flag is on.
1464b88c807SRodney W. GrimesIf
1474b88c807SRodney W. Grimes.Ar file
1484b88c807SRodney W. Grimesis a directory, true indicates that
1494b88c807SRodney W. Grimes.Ar file
1504b88c807SRodney W. Grimescan be searched.
1514b88c807SRodney W. Grimes.It Fl z Ar string
1524b88c807SRodney W. GrimesTrue if the length of
1534b88c807SRodney W. Grimes.Ar string
1544b88c807SRodney W. Grimesis zero.
155717a08b6SSheldon Hearn.It Fl L Ar file
156717a08b6SSheldon HearnTrue if
157717a08b6SSheldon Hearn.Ar file
158717a08b6SSheldon Hearnexists and is a symbolic link.
159717a08b6SSheldon Hearn.It Fl O Ar file
160717a08b6SSheldon HearnTrue if
161717a08b6SSheldon Hearn.Ar file
162717a08b6SSheldon Hearnexists and its owner matches the effective user id of this process.
163717a08b6SSheldon Hearn.It Fl G Ar file
164717a08b6SSheldon HearnTrue if
165717a08b6SSheldon Hearn.Ar file
166717a08b6SSheldon Hearnexists and its group matches the effective group id of this process.
167717a08b6SSheldon Hearn.It Fl S Ar file
168717a08b6SSheldon HearnTrue if
169717a08b6SSheldon Hearn.Ar file
170717a08b6SSheldon Hearnexists and is a socket.
171717a08b6SSheldon Hearn.It Ar file1 Fl nt Ar file2
172717a08b6SSheldon HearnTrue if
173717a08b6SSheldon Hearn.Ar file1
174717a08b6SSheldon Hearnexists and is newer than
175717a08b6SSheldon Hearn.Ar file2 .
176717a08b6SSheldon Hearn.It Ar file1 Fl ot Ar file2
177717a08b6SSheldon HearnTrue if
178717a08b6SSheldon Hearn.Ar file1
179717a08b6SSheldon Hearnexists and is older than
180717a08b6SSheldon Hearn.Ar file2 .
181717a08b6SSheldon Hearn.It Ar file1 Fl ef Ar file2
182717a08b6SSheldon HearnTrue if
183717a08b6SSheldon Hearn.Ar file1
184717a08b6SSheldon Hearnand
185717a08b6SSheldon Hearn.Ar file2
186717a08b6SSheldon Hearnexist and refer to the same file.
1874b88c807SRodney W. Grimes.It Ar string
1884b88c807SRodney W. GrimesTrue if
1894b88c807SRodney W. Grimes.Ar string
1904b88c807SRodney W. Grimesis not the null
1914b88c807SRodney W. Grimesstring.
1927eb5016aSRuslan Ermilov.It Ar s1 Cm = Ar s2
1934b88c807SRodney W. GrimesTrue if the strings
1947eb5016aSRuslan Ermilov.Ar s1
1954b88c807SRodney W. Grimesand
1967eb5016aSRuslan Ermilov.Ar s2
1974b88c807SRodney W. Grimesare identical.
1987eb5016aSRuslan Ermilov.It Ar s1 Cm != Ar s2
1994b88c807SRodney W. GrimesTrue if the strings
2007eb5016aSRuslan Ermilov.Ar s1
2014b88c807SRodney W. Grimesand
2027eb5016aSRuslan Ermilov.Ar s2
2034b88c807SRodney W. Grimesare not identical.
2047eb5016aSRuslan Ermilov.It Ar s1 Cm < Ar s2
205717a08b6SSheldon HearnTrue if string
2067eb5016aSRuslan Ermilov.Ar s1
207717a08b6SSheldon Hearncomes before
2087eb5016aSRuslan Ermilov.Ar s2
209ee70653eSTim J. Robbinsbased on the binary value of their characters.
2107eb5016aSRuslan Ermilov.It Ar s1 Cm > Ar s2
211717a08b6SSheldon HearnTrue if string
2127eb5016aSRuslan Ermilov.Ar s1
213717a08b6SSheldon Hearncomes after
2147eb5016aSRuslan Ermilov.Ar s2
215ee70653eSTim J. Robbinsbased on the binary value of their characters.
2167eb5016aSRuslan Ermilov.It Ar n1 Fl eq Ar n2
2174b88c807SRodney W. GrimesTrue if the integers
2187eb5016aSRuslan Ermilov.Ar n1
2194b88c807SRodney W. Grimesand
2207eb5016aSRuslan Ermilov.Ar n2
2214b88c807SRodney W. Grimesare algebraically
2224b88c807SRodney W. Grimesequal.
2237eb5016aSRuslan Ermilov.It Ar n1 Fl ne Ar n2
2244b88c807SRodney W. GrimesTrue if the integers
2257eb5016aSRuslan Ermilov.Ar n1
2264b88c807SRodney W. Grimesand
2277eb5016aSRuslan Ermilov.Ar n2
2284b88c807SRodney W. Grimesare not
2294b88c807SRodney W. Grimesalgebraically equal.
2307eb5016aSRuslan Ermilov.It Ar n1 Fl gt Ar n2
2314b88c807SRodney W. GrimesTrue if the integer
2327eb5016aSRuslan Ermilov.Ar n1
2334b88c807SRodney W. Grimesis algebraically
2344b88c807SRodney W. Grimesgreater than the integer
2357eb5016aSRuslan Ermilov.Ar n2 .
2367eb5016aSRuslan Ermilov.It Ar n1 Fl ge Ar n2
2374b88c807SRodney W. GrimesTrue if the integer
2387eb5016aSRuslan Ermilov.Ar n1
2394b88c807SRodney W. Grimesis algebraically
2404b88c807SRodney W. Grimesgreater than or equal to the integer
2417eb5016aSRuslan Ermilov.Ar n2 .
2427eb5016aSRuslan Ermilov.It Ar n1 Fl lt Ar n2
2434b88c807SRodney W. GrimesTrue if the integer
2447eb5016aSRuslan Ermilov.Ar n1
2454b88c807SRodney W. Grimesis algebraically less
2464b88c807SRodney W. Grimesthan the integer
2477eb5016aSRuslan Ermilov.Ar n2 .
2487eb5016aSRuslan Ermilov.It Ar n1 Fl le Ar n2
2494b88c807SRodney W. GrimesTrue if the integer
2507eb5016aSRuslan Ermilov.Ar n1
2514b88c807SRodney W. Grimesis algebraically less
2524b88c807SRodney W. Grimesthan or equal to the integer
2537eb5016aSRuslan Ermilov.Ar n2 .
2544b88c807SRodney W. Grimes.El
2554b88c807SRodney W. Grimes.Pp
256593cc6f3SYaroslav TykhiyIf
257593cc6f3SYaroslav Tykhiy.Ar file
258593cc6f3SYaroslav Tykhiyis a symbolic link,
259593cc6f3SYaroslav Tykhiy.Nm
260593cc6f3SYaroslav Tykhiywill fully dereference it and then evaluate the expression
261593cc6f3SYaroslav Tykhiyagainst the file referenced, except for the
262593cc6f3SYaroslav Tykhiy.Fl h
263593cc6f3SYaroslav Tykhiyand
264593cc6f3SYaroslav Tykhiy.Fl L
265593cc6f3SYaroslav Tykhiyprimaries.
266593cc6f3SYaroslav Tykhiy.Pp
2674b88c807SRodney W. GrimesThese primaries can be combined with the following operators:
2684b88c807SRodney W. Grimes.Bl -tag -width Ar
2694b88c807SRodney W. Grimes.It Cm \&! Ar expression
2704b88c807SRodney W. GrimesTrue if
2714b88c807SRodney W. Grimes.Ar expression
2724b88c807SRodney W. Grimesis false.
2734b88c807SRodney W. Grimes.It Ar expression1 Fl a Ar expression2
2744b88c807SRodney W. GrimesTrue if both
2754b88c807SRodney W. Grimes.Ar expression1
2764b88c807SRodney W. Grimesand
2774b88c807SRodney W. Grimes.Ar expression2
2784b88c807SRodney W. Grimesare true.
2794b88c807SRodney W. Grimes.It Ar expression1 Fl o Ar expression2
2804b88c807SRodney W. GrimesTrue if either
2814b88c807SRodney W. Grimes.Ar expression1
2824b88c807SRodney W. Grimesor
2834b88c807SRodney W. Grimes.Ar expression2
2844b88c807SRodney W. Grimesare true.
2854b88c807SRodney W. Grimes.It Cm \&( Ns Ar expression Ns Cm \&)
2864b88c807SRodney W. GrimesTrue if expression is true.
2874b88c807SRodney W. Grimes.El
2884b88c807SRodney W. Grimes.Pp
2894b88c807SRodney W. GrimesThe
2904b88c807SRodney W. Grimes.Fl a
2914b88c807SRodney W. Grimesoperator has higher precedence than the
2924b88c807SRodney W. Grimes.Fl o
2934b88c807SRodney W. Grimesoperator.
294d90c5c4aSAkinori MUSHA.Pp
295d90c5c4aSAkinori MUSHASome shells may provide a builtin
296d90c5c4aSAkinori MUSHA.Nm
297d90c5c4aSAkinori MUSHAcommand which is similar or identical to this utility.
298d90c5c4aSAkinori MUSHAConsult the
299d90c5c4aSAkinori MUSHA.Xr builtin 1
300d90c5c4aSAkinori MUSHAmanual page.
3014b88c807SRodney W. Grimes.Sh GRAMMAR AMBIGUITY
3024b88c807SRodney W. GrimesThe
303be8b1497SRuslan Ermilov.Nm
304eccea571SRuslan Ermilovgrammar is inherently ambiguous.
305eccea571SRuslan ErmilovIn order to assure a degree of consistency,
3064b88c807SRodney W. Grimesthe cases described in the
3074b88c807SRodney W. Grimes.St -p1003.2 ,
3084b88c807SRodney W. Grimessection D11.2/4.62.4, standard
3094b88c807SRodney W. Grimesare evaluated consistently according to the rules specified in the
310eccea571SRuslan Ermilovstandards document.
311eccea571SRuslan ErmilovAll other cases are subject to the ambiguity in the
3124b88c807SRodney W. Grimescommand semantics.
3134b88c807SRodney W. Grimes.Sh RETURN VALUES
3144b88c807SRodney W. GrimesThe
315be8b1497SRuslan Ermilov.Nm
3164b88c807SRodney W. Grimesutility exits with one of the following values:
3177eb5016aSRuslan Ermilov.Bl -tag -width indent
3184b88c807SRodney W. Grimes.It 0
3194b88c807SRodney W. Grimesexpression evaluated to true.
3204b88c807SRodney W. Grimes.It 1
3214b88c807SRodney W. Grimesexpression evaluated to false or expression was
3224b88c807SRodney W. Grimesmissing.
3234b88c807SRodney W. Grimes.It >1
3244b88c807SRodney W. GrimesAn error occurred.
3254b88c807SRodney W. Grimes.El
326e5bf7bf4SJoseph Koshy.Sh SEE ALSO
327d90c5c4aSAkinori MUSHA.Xr builtin 1 ,
328e5bf7bf4SJoseph Koshy.Xr expr 1 ,
329593cc6f3SYaroslav Tykhiy.Xr sh 1 ,
330593cc6f3SYaroslav Tykhiy.Xr symlink 7
3314b88c807SRodney W. Grimes.Sh STANDARDS
3324b88c807SRodney W. GrimesThe
333be8b1497SRuslan Ermilov.Nm
334717a08b6SSheldon Hearnutility implements a superset of the
3354b88c807SRodney W. Grimes.St -p1003.2
336717a08b6SSheldon Hearnspecification.
337ec45d514SYaroslav Tykhiy.Sh BUGS
338ec45d514SYaroslav TykhiyBoth sides are always evaluated in
339ec45d514SYaroslav Tykhiy.Fl a
340ec45d514SYaroslav Tykhiyand
341ec45d514SYaroslav Tykhiy.Fl o ,
342ec45d514SYaroslav Tykhiyunlike in the logical operators of
343ec45d514SYaroslav Tykhiy.Xr sh 1 .
3446a215886SYaroslav TykhiyFor instance, the writable status of
3456a215886SYaroslav Tykhiy.Pa file
3466a215886SYaroslav Tykhiywill be tested by the following command even though the former expression
3476a215886SYaroslav Tykhiyindicated false, which results in a gratuitous access to the file system:
3486a215886SYaroslav Tykhiy.Pp
3496a215886SYaroslav Tykhiy.Dl "[ -z abc -a -w file ]"
350