summary refs log tree commit diff
path: root/bin/1sh/1sh-test.1
blob: 8ad8d0f4989dda7e7d62c3387b5ba536cb45adec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
.\"-
.\" 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.
.\" 3. 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
.\" $FreeBSD: releng/12.1/bin/test/test.1 314436 2017-02-28 23:42:47Z imp $
.\"
.Dd October 5, 2016
.Dt 1SH-TEST 1
.Os
.Sh NAME
.Nm test ,
.Nm \&[
.Nd condition evaluation utility
.Sh SYNOPSIS
.Nm
.Ar expression
.Nm \&[
.Ar expression Cm \&]
.Sh DESCRIPTION
The
.Nm
utility evaluates the expression and, if it evaluates
to true, returns a zero (true) exit status; otherwise
it returns 1 (false).
If there is no expression,
.Nm
also
returns 1 (false).
.Pp
All operators and flags are separate arguments to the
.Nm
utility.
.Pp
The following primaries are used to construct expression:
.Bl -tag -width Ar
.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.
This operator is retained for compatibility with previous versions of
this program.
Do not rely on its existence; use
.Fl L
instead.
.It Fl k Ar file
True if
.Ar file
exists and its sticky bit is set.
.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
.Pq Tn FIFO .
.It Fl r Ar file
True if
.Ar file
exists and is readable.
.It Fl s Ar file
True if
.Ar file
exists and has a size greater
than zero.
.It Fl t Ar file_descriptor
True if the file whose file descriptor number
is
.Ar file_descriptor
is open and is associated with a terminal.
.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.
.It Fl L Ar file
True if
.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
True if
.Ar file1
exists and is newer than
.Ar file2 .
.It Ar file1 Fl ot Ar file2
True if
.Ar file1
exists and is older than
.Ar file2 .
.It Ar file1 Fl ef Ar file2
True if
.Ar file1
and
.Ar file2
exist and refer to the same file.
.It Ar string
True if
.Ar string
is not the null
string.
.It Ar s1 Cm = Ar s2
True if the strings
.Ar s1
and
.Ar s2
are identical.
.It Ar s1 Cm != Ar s2
True if the strings
.Ar s1
and
.Ar s2
are not identical.
.It Ar s1 Cm < Ar s2
True if string
.Ar s1
comes before
.Ar s2
based on the binary value of their characters.
.It Ar s1 Cm > Ar s2
True if string
.Ar s1
comes after
.Ar s2
based on the binary value of their characters.
.It Ar n1 Fl eq Ar n2
True if the integers
.Ar n1
and
.Ar n2
are algebraically
equal.
.It Ar n1 Fl ne Ar n2
True if the integers
.Ar n1
and
.Ar n2
are not
algebraically equal.
.It Ar n1 Fl gt Ar n2
True if the integer
.Ar n1
is algebraically
greater than the integer
.Ar n2 .
.It Ar n1 Fl ge Ar n2
True if the integer
.Ar n1
is algebraically
greater than or equal to the integer
.Ar n2 .
.It Ar n1 Fl lt Ar n2
True if the integer
.Ar n1
is algebraically less
than the integer
.Ar n2 .
.It Ar n1 Fl le Ar n2
True if the integer
.Ar n1
is algebraically less
than or equal to the integer
.Ar n2 .
.El
.Pp
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
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.
.It Cm \&( Ar expression Cm \&)
True if expression is true.
.El
.Pp
The
.Fl a
operator has higher precedence than the
.Fl o
operator.
.Pp
Some shells may provide a builtin
.Nm
command which is similar or identical to this utility.
Consult the
.Xr builtin 1
manual page.
.Sh GRAMMAR AMBIGUITY
The
.Nm
grammar is inherently ambiguous.
In order to assure a degree of consistency,
the cases described in the
.St -p1003.2 ,
section D11.2/4.62.4, standard
are evaluated consistently according to the rules specified in the
standards document.
All other cases are subject to the ambiguity in the
command semantics.
.Pp
In particular, only expressions containing
.Fl a ,
.Fl o ,
.Cm \&(
or
.Cm \&)
can be ambiguous.
.Sh EXIT STATUS
The
.Nm
utility exits with one of the following values:
.Bl -tag -width indent
.It 0
expression evaluated to true.
.It 1
expression evaluated to false or expression was
missing.
.It >1
An error occurred.
.El
.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.
.Sh COMPATIBILITY
For compatibility with some other implementations,
the
.Cm =
primary can be substituted with
.Cm ==
with the same meaning.
.Sh SEE ALSO
.Xr builtin 1 ,
.Xr expr 1 ,
.Xr find 1 ,
.Xr sh 1 ,
.Xr stat 1 ,
.Xr symlink 7
.Sh STANDARDS
The
.Nm
utility implements a superset of the
.St -p1003.2
specification.
The primaries
.Cm < ,
.Cm == ,
.Cm > ,
.Fl ef ,
.Fl nt ,
.Fl ot ,
.Fl G ,
and
.Fl O
are extensions.
.Sh HISTORY
A
.Nm
utility appeared in
.At v7 .
.Sh BUGS
Both sides are always evaluated in
.Fl a
and
.Fl o .
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 ]"
To avoid this, write
.Dl "[ -z abc ] && [ -w file ]"