Un outil pour savoir si une commande est posix - retour accueil
git clone git://bebou.netlib.re/isposix
Log | Files | Refs | README |
test.html (32050B)
1 <!-- Copyright 2001-2024 IEEE and The Open Group, All Rights Reserved --> 2 <!DOCTYPE HTML> 3 <html lang="en"> 4 <head> 5 <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.8.0"> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group rhtm tool v1.2.4 --> 8 <!-- Copyright (c) 2001-2024 The Open Group, All Rights Reserved --> 9 <title>test</title> 10 </head> 11 <body bgcolor="white"> 12 <div class="NAVHEADER"> 13 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0"> 14 <tr class="nav"> 15 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/tee.html" accesskey="P"><<< 16 Previous</a></td> 17 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td> 18 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/time.html" accesskey="N">Next >>></a></td> 19 </tr> 20 </table> 21 <hr align="left" width="100%"></div> 22 <script language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> 23 <center><font size="2">The Open Group Base Specifications Issue 8<br> 24 IEEE Std 1003.1-2024<br> 25 Copyright © 2001-2024 The IEEE and The Open Group</font></center> 26 <hr size="2" noshade> 27 <a name="top" id="top"></a> <a name="test" id="test"></a> <a name="tag_20_121" id="tag_20_121"></a><!-- test --> 28 <h4 class="mansect"><a name="tag_20_121_01" id="tag_20_121_01"></a>NAME</h4> 29 <blockquote>test — evaluate expression</blockquote> 30 <h4 class="mansect"><a name="tag_20_121_02" id="tag_20_121_02"></a>SYNOPSIS</h4> 31 <blockquote class="synopsis"> 32 <p><code><tt>test</tt> <b>[</b><i>expression</i><b>]</b> <tt><br> 33 <br> 34 [</tt> <b>[</b><i>expression</i><b>]</b> <tt>]<br></tt></code></p> 35 </blockquote> 36 <h4 class="mansect"><a name="tag_20_121_03" id="tag_20_121_03"></a>DESCRIPTION</h4> 37 <blockquote> 38 <p>The <i>test</i> utility shall evaluate the <i>expression</i> and indicate the result of the evaluation by its exit status. An 39 exit status of zero indicates that the expression evaluated as true and an exit status of 1 indicates that the expression evaluated 40 as false.</p> 41 <p>In the second form of the utility, where the utility name used is <i>[</i> rather than <i>test</i>, the application shall ensure 42 that the closing square bracket is a separate argument. The <i>test</i> and <i>[</i> utilities may be implemented as a single 43 linked utility which examines the basename of the zeroth command line argument to determine whether to behave as the <i>test</i> or 44 <i>[</i> variant. Applications using the <i>exec</i> family of functions to execute these utilities shall ensure that the argument 45 passed in <i>arg0</i> or <i>argv</i>[0] is <tt>'['</tt> when executing the <i>[</i> utility and has a basename of <tt>"test"</tt> 46 when executing the <i>test</i> utility.</p> 47 </blockquote> 48 <h4 class="mansect"><a name="tag_20_121_04" id="tag_20_121_04"></a>OPTIONS</h4> 49 <blockquote> 50 <p>The <i>test</i> utility shall not recognize the <tt>"--"</tt> argument in the manner specified by Guideline 10 in XBD <a href= 51 "../basedefs/V1_chap12.html#tag_12_02"><i>12.2 Utility Syntax Guidelines</i></a> . In addition, when the utility name used is 52 <i>[</i> the utility does not conform to Guidelines 1 and 2.</p> 53 <p>No options shall be supported.</p> 54 </blockquote> 55 <h4 class="mansect"><a name="tag_20_121_05" id="tag_20_121_05"></a>OPERANDS</h4> 56 <blockquote> 57 <p>The application shall ensure that all operators and elements of primaries are presented as separate arguments to the <i>test</i> 58 utility.</p> 59 <p>The following primaries can be used to construct <i>expression</i>:</p> 60 <dl compact> 61 <dd></dd> 62 <dt><b>-b </b><i>pathname</i></dt> 63 <dd>True if <i>pathname</i> resolves to an existing directory entry for a block special file. False if <i>pathname</i> cannot be 64 resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that is not a block special file.</dd> 65 <dt><b>-c </b><i>pathname</i></dt> 66 <dd>True if <i>pathname</i> resolves to an existing directory entry for a character special file. False if <i>pathname</i> cannot 67 be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that is not a character special file.</dd> 68 <dt><b>-d </b><i>pathname</i></dt> 69 <dd>True if <i>pathname</i> resolves to an existing directory entry for a directory. False if <i>pathname</i> cannot be resolved, 70 or if <i>pathname</i> resolves to an existing directory entry for a file that is not a directory.</dd> 71 <dt><b>-e </b><i>pathname</i></dt> 72 <dd>True if <i>pathname</i> resolves to an existing directory entry. False if <i>pathname</i> cannot be resolved.</dd> 73 <dt><i>pathname1</i><b> -ef </b><i>pathname2</i></dt> 74 <dd><br> 75 True if <i>pathname1</i> and <i>pathname2</i> resolve to existing directory entries for the same file; otherwise, false.</dd> 76 <dt><b>-f </b><i>pathname</i></dt> 77 <dd>True if <i>pathname</i> resolves to an existing directory entry for a regular file. False if <i>pathname</i> cannot be 78 resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that is not a regular file.</dd> 79 <dt><b>-g </b><i>pathname</i></dt> 80 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file that has its set-group-ID flag set. False if 81 <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that does not have its 82 set-group-ID flag set.</dd> 83 <dt><b>-h </b><i>pathname</i></dt> 84 <dd>True if <i>pathname</i> resolves to an existing directory entry for a symbolic link. False if <i>pathname</i> cannot be 85 resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that is not a symbolic link. If the final 86 component of <i>pathname</i> is a symbolic link, that symbolic link is not followed.</dd> 87 <dt><b>-L </b><i>pathname</i></dt> 88 <dd>True if <i>pathname</i> resolves to an existing directory entry for a symbolic link. False if <i>pathname</i> cannot be 89 resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that is not a symbolic link. If the final 90 component of <i>pathname</i> is a symbolic link, that symbolic link is not followed.</dd> 91 <dt><b>-n </b><i>string</i></dt> 92 <dd>True if the length of <i>string</i> is non-zero; otherwise, false.</dd> 93 <dt><i>pathname1</i><b> -nt </b><i>pathname2</i></dt> 94 <dd><br> 95 True if <i>pathname1</i> resolves to an existing file and <i>pathname2</i> cannot be resolved, or if both resolve to existing files 96 and <i>pathname1</i> is newer than <i>pathname2</i> according to their last data modification timestamps; otherwise, false.</dd> 97 <dt><i>pathname1</i><b> -ot </b><i>pathname2</i></dt> 98 <dd><br> 99 True if <i>pathname2</i> resolves to an existing file and <i>pathname1</i> cannot be resolved, or if both resolve to existing files 100 and <i>pathname1</i> is older than <i>pathname2</i> according to their last data modification timestamps; otherwise, false.</dd> 101 <dt><b>-p </b><i>pathname</i></dt> 102 <dd>True if <i>pathname</i> resolves to an existing directory entry for a FIFO. False if <i>pathname</i> cannot be resolved, or if 103 <i>pathname</i> resolves to an existing directory entry for a file that is not a FIFO.</dd> 104 <dt><b>-r </b><i>pathname</i></dt> 105 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file for which permission to read from the file is 106 granted, as defined in <a href="../utilities/V3_chap01.html#tag_18_01_01_04"><i>1.1.1.4 File Read, Write, and Creation</i></a> . 107 False if <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file for which 108 permission to read from the file is not granted.</dd> 109 <dt><b>-S </b><i>pathname</i></dt> 110 <dd>True if <i>pathname</i> resolves to an existing directory entry for a socket. False if <i>pathname</i> cannot be resolved, or 111 if <i>pathname</i> resolves to an existing directory entry for a file that is not a socket.</dd> 112 <dt><b>-s </b><i>pathname</i></dt> 113 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file that has a size greater than zero. False if 114 <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that does not have a 115 size greater than zero.</dd> 116 <dt><b>-t </b><i>file_descriptor</i></dt> 117 <dd><br> 118 True if file descriptor number <i>file_descriptor</i> is open and is associated with a terminal. False if <i>file_descriptor</i> is 119 not a valid file descriptor number, or if file descriptor number <i>file_descriptor</i> is not open, or if it is open but is not 120 associated with a terminal.</dd> 121 <dt><b>-u </b><i>pathname</i></dt> 122 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file that has its set-user-ID flag set. False if 123 <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file that does not have its 124 set-user-ID flag set.</dd> 125 <dt><b>-w </b><i>pathname</i></dt> 126 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file for which permission to write to the file is 127 granted, as defined in <a href="../utilities/V3_chap01.html#tag_18_01_01_04"><i>1.1.1.4 File Read, Write, and Creation</i></a> . 128 False if <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory entry for a file for which 129 permission to write to the file is not granted.</dd> 130 <dt><b>-x </b><i>pathname</i></dt> 131 <dd>True if <i>pathname</i> resolves to an existing directory entry for a file for which permission to execute the file (or search 132 it, if it is a directory) is granted, as defined in <a href="../utilities/V3_chap01.html#tag_18_01_01_04"><i>1.1.1.4 File Read, 133 Write, and Creation</i></a> . False if <i>pathname</i> cannot be resolved, or if <i>pathname</i> resolves to an existing directory 134 entry for a file for which permission to execute (or search) the file is not granted.</dd> 135 <dt><b>-z </b><i>string</i></dt> 136 <dd>True if the length of string <i>string</i> is zero; otherwise, false.</dd> 137 <dt><i>string</i></dt> 138 <dd>True if the string <i>string</i> is not the null string; otherwise, false.</dd> 139 <dt><i>s1</i><b> = </b><i>s2</i></dt> 140 <dd>True if the strings <i>s1</i> and <i>s2</i> are identical; otherwise, false.</dd> 141 <dt><i>s1</i><b> != </b><i>s2</i></dt> 142 <dd>True if the strings <i>s1</i> and <i>s2</i> are not identical; otherwise, false.</dd> 143 <dt><i>s1</i><b> > </b><i>s2</i></dt> 144 <dd>True if <i>s1</i> collates after <i>s2</i> in the current locale; otherwise, false.</dd> 145 <dt><i>s1</i><b> < </b><i>s2</i></dt> 146 <dd>True if <i>s1</i> collates before <i>s2</i> in the current locale; otherwise, false.</dd> 147 <dt><i>n1</i><b> -eq </b><i>n2</i></dt> 148 <dd>True if the integers <i>n1</i> and <i>n2</i> are algebraically equal; otherwise, false.</dd> 149 <dt><i>n1</i><b> -ne </b><i>n2</i></dt> 150 <dd>True if the integers <i>n1</i> and <i>n2</i> are not algebraically equal; otherwise, false.</dd> 151 <dt><i>n1</i><b> -gt </b><i>n2</i></dt> 152 <dd>True if the integer <i>n1</i> is algebraically greater than the integer <i>n2</i>; otherwise, false.</dd> 153 <dt><i>n1</i><b> -ge </b><i>n2</i></dt> 154 <dd>True if the integer <i>n1</i> is algebraically greater than or equal to the integer <i>n2</i>; otherwise, false.</dd> 155 <dt><i>n1</i><b> -lt </b><i>n2</i></dt> 156 <dd>True if the integer <i>n1</i> is algebraically less than the integer <i>n2</i>; otherwise, false.</dd> 157 <dt><i>n1</i><b> -le </b><i>n2</i></dt> 158 <dd>True if the integer <i>n1</i> is algebraically less than or equal to the integer <i>n2</i>; otherwise, false.</dd> 159 </dl> 160 <p>With the exception of the <b>-h</b> <i>pathname</i> and <b>-L</b> <i>pathname</i> primaries, if a <i>pathname</i>, 161 <i>pathname1</i>, or <i>pathname2</i> argument is a symbolic link, <i>test</i> shall evaluate the expression by resolving the 162 symbolic link and using the file referenced by the link.</p> 163 <p>These primaries can be combined with the following operator:</p> 164 <dl compact> 165 <dd></dd> 166 <dt><b>! </b><i>expression</i></dt> 167 <dd>True if <i>expression</i> is false. False if <i>expression</i> is true.</dd> 168 </dl> 169 <p>The primaries with two elements of the form:</p> 170 <pre> 171 <tt>-</tt><i>primary_operator primary_operand</i><tt> 172 </tt></pre> 173 <p>are known as <i>unary primaries</i>. The primaries with three elements in either of the two forms:</p> 174 <pre> 175 <i>primary_operand </i><tt>-</tt><i>primary_operator primary_operand 176 <br> 177 primary_operand primary_operator primary_operand</i><tt> 178 </tt></pre> 179 <p>are known as <i>binary primaries</i>. Additional implementation-defined operators and <i>primary_operator</i>s may be provided 180 by implementations. They shall be of the form -<i>operator</i> where the first character of <i>operator</i> is not a digit.</p> 181 <p>The algorithm for determining the precedence of the operators and the return value that shall be generated is based on the 182 number of arguments presented to <i>test</i>. (However, when using the <tt>"[...]"</tt> form, the <right-square-bracket> 183 final argument shall not be counted in this algorithm.)</p> 184 <p>In the following list, $1, $2, $3, and $4 represent the arguments presented to <i>test</i>:</p> 185 <dl compact> 186 <dd></dd> 187 <dt>0 arguments:</dt> 188 <dd>Exit false (1).</dd> 189 <dt>1 argument:</dt> 190 <dd>Exit true (0) if $1 is not null; otherwise, exit false.</dd> 191 <dt>2 arguments:</dt> 192 <dd> 193 <ul> 194 <li> 195 <p>If $1 is <tt>'!'</tt>, exit true if $2 is null, false if $2 is not null.</p> 196 </li> 197 <li> 198 <p>If $1 is a unary primary, exit true if the unary test is true, false if the unary test is false.</p> 199 </li> 200 <li> 201 <p>Otherwise, produce unspecified results.</p> 202 </li> 203 </ul> 204 </dd> 205 <dt>3 arguments:</dt> 206 <dd> 207 <ul> 208 <li> 209 <p>If $2 is a binary primary, perform the binary test of $1 and $3.</p> 210 </li> 211 <li> 212 <p>If $1 is <tt>'!'</tt>, negate the two-argument test of $2 and $3.</p> 213 </li> 214 <li> 215 <p>Otherwise, produce unspecified results.</p> 216 </li> 217 </ul> 218 </dd> 219 <dt>4 arguments:</dt> 220 <dd> 221 <ul> 222 <li> 223 <p>If $1 is <tt>'!'</tt>, negate the three-argument test of $2, $3, and $4.</p> 224 </li> 225 <li> 226 <p>Otherwise, the results are unspecified.</p> 227 </li> 228 </ul> 229 </dd> 230 <dt>>4 arguments:</dt> 231 <dd>The results are unspecified.</dd> 232 </dl> 233 </blockquote> 234 <h4 class="mansect"><a name="tag_20_121_06" id="tag_20_121_06"></a>STDIN</h4> 235 <blockquote> 236 <p>Not used.</p> 237 </blockquote> 238 <h4 class="mansect"><a name="tag_20_121_07" id="tag_20_121_07"></a>INPUT FILES</h4> 239 <blockquote> 240 <p>None.</p> 241 </blockquote> 242 <h4 class="mansect"><a name="tag_20_121_08" id="tag_20_121_08"></a>ENVIRONMENT VARIABLES</h4> 243 <blockquote> 244 <p>The following environment variables shall affect the execution of <i>test</i>:</p> 245 <dl compact> 246 <dd></dd> 247 <dt><i>LANG</i></dt> 248 <dd>Provide a default value for the internationalization variables that are unset or null. (See XBD <a href= 249 "../basedefs/V1_chap08.html#tag_08_02"><i>8.2 Internationalization Variables</i></a> for the precedence of internationalization 250 variables used to determine the values of locale categories.)</dd> 251 <dt><i>LC_ALL</i></dt> 252 <dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd> 253 <dt><i>LC_COLLATE</i></dt> 254 <dd><br> 255 Determine the locale for the behavior of the <b>></b> and <b><</b> string comparison operators.</dd> 256 <dt><i>LC_CTYPE</i></dt> 257 <dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as 258 opposed to multi-byte characters in arguments).</dd> 259 <dt><i>LC_MESSAGES</i></dt> 260 <dd><br> 261 Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.</dd> 262 <dt><i>NLSPATH</i></dt> 263 <dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> 264 Determine the location of messages objects and message catalogs. <img src="../images/opt-end.gif" alt="[Option End]" border= 265 "0"></dd> 266 </dl> 267 </blockquote> 268 <h4 class="mansect"><a name="tag_20_121_09" id="tag_20_121_09"></a>ASYNCHRONOUS EVENTS</h4> 269 <blockquote> 270 <p>Default.</p> 271 </blockquote> 272 <h4 class="mansect"><a name="tag_20_121_10" id="tag_20_121_10"></a>STDOUT</h4> 273 <blockquote> 274 <p>Not used.</p> 275 </blockquote> 276 <h4 class="mansect"><a name="tag_20_121_11" id="tag_20_121_11"></a>STDERR</h4> 277 <blockquote> 278 <p>The standard error shall be used only for diagnostic messages.</p> 279 </blockquote> 280 <h4 class="mansect"><a name="tag_20_121_12" id="tag_20_121_12"></a>OUTPUT FILES</h4> 281 <blockquote> 282 <p>None.</p> 283 </blockquote> 284 <h4 class="mansect"><a name="tag_20_121_13" id="tag_20_121_13"></a>EXTENDED DESCRIPTION</h4> 285 <blockquote> 286 <p>None.</p> 287 </blockquote> 288 <h4 class="mansect"><a name="tag_20_121_14" id="tag_20_121_14"></a>EXIT STATUS</h4> 289 <blockquote> 290 <p>The following exit values shall be returned:</p> 291 <dl compact> 292 <dd></dd> 293 <dt> 0</dt> 294 <dd><i>expression</i> evaluated to true.</dd> 295 <dt> 1</dt> 296 <dd><i>expression</i> evaluated to false or <i>expression</i> was missing.</dd> 297 <dt>>1</dt> 298 <dd>An error occurred.</dd> 299 </dl> 300 </blockquote> 301 <h4 class="mansect"><a name="tag_20_121_15" id="tag_20_121_15"></a>CONSEQUENCES OF ERRORS</h4> 302 <blockquote> 303 <p>Default.</p> 304 </blockquote> 305 <hr> 306 <div class="box"><em>The following sections are informative.</em></div> 307 <h4 class="mansect"><a name="tag_20_121_16" id="tag_20_121_16"></a>APPLICATION USAGE</h4> 308 <blockquote> 309 <p>Since <tt>'>'</tt> and <tt>'<'</tt> are operators in the shell language, applications need to quote them when passing them 310 as arguments to <i>test</i> from a shell.</p> 311 <p>The <b>-a</b> and <b>-o</b> binary primaries and the <tt>'('</tt> and <tt>')'</tt> operators have been removed. (Many 312 expressions using them were ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts 313 using these expressions should be converted to the forms given below. Even though many implementations will continue to support 314 these forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other 315 primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations 316 like:</p> 317 <pre> 318 <tt>test "$1" -a "$2" 319 </tt></pre> 320 <p>should be written as:</p> 321 <pre> 322 <tt>test "$1" && test "$2" 323 </tt></pre> 324 <p>to avoid problems if a user supplied values such as $1 set to <tt>'!'</tt> and $2 set to the null string. That is, replace:</p> 325 <pre> 326 <tt>test expr1 -a expr2 327 </tt></pre> 328 <p>with:</p> 329 <pre> 330 <tt>test expr1 && test expr2 331 </tt></pre> 332 <p>and replace:</p> 333 <pre> 334 <tt>test expr1 -o expr2 335 </tt></pre> 336 <p>with:</p> 337 <pre> 338 <tt>test expr1 || test expr2 339 </tt></pre> 340 <p>but note that, in <i>test</i>, <b>-a</b> was specified as having higher precedence than <b>-o</b> while <tt>"&&"</tt> 341 and <tt>"||"</tt> have equal precedence in the shell.</p> 342 <p>Parentheses or braces can be used in the shell command language to effect grouping.</p> 343 <p>The two commands:</p> 344 <pre> 345 <tt>test "$1" 346 test ! "$1" 347 </tt></pre> 348 <p>could not be used reliably on some historical systems. Unexpected results would occur if such a <i>string</i> expression were 349 used and $1 expanded to <tt>'!'</tt>, <tt>'('</tt>, or a known unary primary. Better constructs are:</p> 350 <pre> 351 <tt>test -n "$1" 352 test -z "$1" 353 </tt></pre> 354 respectively. 355 <p>Historical systems have also been unreliable given the common construct:</p> 356 <pre> 357 <tt>test "$response" = "expected string" 358 </tt></pre> 359 <p>One of the following is a more reliable form:</p> 360 <pre> 361 <tt>test "X$response" = "Xexpected string" 362 test "expected string" = "$response" 363 </tt></pre> 364 <p>Note that the second form assumes that <i>expected string</i> could not be confused with any unary primary. If <i>expected 365 string</i> starts with <tt>'-'</tt>, <tt>'('</tt>, <tt>'!'</tt>, or even <tt>'='</tt>, the first form should be used instead. Using 366 the preceding rules, any of the three comparison forms is reliable, given any input. (However, note that the strings are quoted in 367 all cases.)</p> 368 <p>Historically, the string comparison binary primaries, <tt>'='</tt> and <tt>"!="</tt>, had a higher precedence than any unary 369 primary in the greater than 4 argument case, and consequently unexpected results could occur if arguments were not properly 370 prepared. For example, in:</p> 371 <pre> 372 <tt>test -d "$1" -o -d "$2" 373 </tt></pre> 374 <p>If $1 evaluates to a possible directory name of <tt>'='</tt>, the first three arguments are considered a string comparison, 375 which causes a syntax error when the second <b>-d</b> is encountered. The following form prevents this:</p> 376 <pre> 377 <tt>test -d "$1" || test -d "$2" 378 </tt></pre> 379 <p>Also in the greater than 4 argument case:</p> 380 <pre> 381 <tt>test "$1" = "bat" -a "$2" = "ball" 382 </tt></pre> 383 <p>syntax errors would occur if $1 evaluates to <tt>'('</tt> or <tt>'!'</tt>. One of the following forms prevents this; the second 384 is preferred:</p> 385 <pre> 386 <tt>test "$1" = "bat" && test "$2" = "ball" 387 test "X$1" = "Xbat" && test "X$2" = "Xball" 388 </tt></pre> 389 <p>Note that none of the following examples are permitted by the syntax described:</p> 390 <pre> 391 <tt>[-f file] 392 [-f file ] 393 [ -f file] 394 [ -f file 395 test -f file ] 396 </tt></pre> 397 <p>In the first two cases, if a utility named <i>[-f</i> exists, that utility would be invoked, and not <i>test</i>. In the 398 remaining cases, the brackets are mismatched, and the behavior is unspecified. However:</p> 399 <pre> 400 <tt>test ! ] 401 </tt></pre> 402 <p>does have a defined meaning, and must exit with status 1. Similarly:</p> 403 <pre> 404 <tt>test ] 405 </tt></pre> 406 <p>must exit with status 0.</p> 407 </blockquote> 408 <h4 class="mansect"><a name="tag_20_121_17" id="tag_20_121_17"></a>EXAMPLES</h4> 409 <blockquote> 410 <ol> 411 <li> 412 <p>Exit if there are not two or three arguments (two variations):</p> 413 <pre> 414 <tt>if [ $# -ne 2 ] && [ $# -ne 3 ]; then exit 1; fi 415 if [ $# -lt 2 ] || [ $# -gt 3 ]; then exit 1; fi 416 </tt></pre></li> 417 <li> 418 <p>Perform a <a href="../utilities/mkdir.html"><i>mkdir</i></a> if a directory does not exist:</p> 419 <pre> 420 <tt>test ! -d tempdir && mkdir tempdir 421 </tt></pre></li> 422 <li> 423 <p>Wait for a file to become non-readable:</p> 424 <pre> 425 <tt>while test -r thefile 426 do 427 sleep 30 428 done 429 echo '"thefile" is no longer readable' 430 </tt></pre></li> 431 <li> 432 <p>Perform a command if the argument is one of three strings (two variations):</p> 433 <pre> 434 <tt>if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ] 435 then 436 </tt><i>command</i><tt> 437 fi 438 <br> 439 case "$1" in 440 pear|grape|apple) </tt><i>command</i><tt> ;; 441 esac 442 </tt></pre></li> 443 </ol> 444 </blockquote> 445 <h4 class="mansect"><a name="tag_20_121_18" id="tag_20_121_18"></a>RATIONALE</h4> 446 <blockquote> 447 <p>The KornShell-derived conditional command (double bracket <b>[[]]</b>) was removed from the shell command language description 448 in an early proposal. Objections were raised that the real problem is misuse of the <i>test</i> command (<b>[</b>), and putting it 449 into the shell is the wrong way to fix the problem. Instead, proper documentation and a new shell reserved word (<b>!</b>) are 450 sufficient. A later proposal to add <b>[[]]</b> in Issue 8 was also rejected because existing implementations of it were found to 451 be error-prone in a similar way to historical versions of <i>test</i>, and there was also too much variation in behavior between 452 shells that support it.</p> 453 <p>Tests that require multiple <i>test</i> operations can be done at the shell level using individual invocations of the 454 <i>test</i> command and shell logicals, rather than using the error-prone historical <b>-a</b> and <b>-o</b> operators of 455 <i>test</i>.</p> 456 <p>The BSD and System V versions of <b>-f</b> were not the same. The BSD definition was:</p> 457 <dl compact> 458 <dd></dd> 459 <dt><b>-f </b><i>file</i></dt> 460 <dd>True if <i>file</i> exists and is not a directory.</dd> 461 </dl> 462 <p>The SVID version (true if the file exists and is a regular file) was chosen for this volume of POSIX.1-2024 because its use is 463 consistent with the <b>-b</b>, <b>-c</b>, <b>-d</b>, and <b>-p</b> operands (<i>file</i> exists and is a specific file type).</p> 464 <p>The <b>-e</b> primary, possessing similar functionality to that provided by the C shell, was added because it provides the only 465 way for a shell script to find out if a file exists without trying to open the file. Since implementations are allowed to add 466 additional file types, a portable script cannot use:</p> 467 <pre> 468 <tt>test -b foo || test -c foo || test -d foo || test -f foo || test -p foo 469 </tt></pre> 470 <p>to find out if <b>foo</b> is an existing file. On historical BSD systems, the existence of a file could be determined by:</p> 471 <pre> 472 <tt>test -f foo || test -d foo 473 </tt></pre> 474 <p>but there was no easy way to determine that an existing file was a regular file. An early proposal used the KornShell <b>-a</b> 475 primary (with the same meaning), but this was changed to <b>-e</b> because there were concerns about the high probability of humans 476 confusing the <b>-a</b> primary with the historical <b>-a</b> binary operator.</p> 477 <p>The following options were not included in this volume of POSIX.1-2024, although they are provided by some implementations. 478 These operands should not be used by new implementations for other purposes:</p> 479 <dl compact> 480 <dd></dd> 481 <dt><b>-k </b><i>file</i></dt> 482 <dd>True if <i>file</i> exists and its sticky bit is set.</dd> 483 <dt><b>-C </b><i>file</i></dt> 484 <dd>True if <i>file</i> is a contiguous file.</dd> 485 <dt><b>-V </b><i>file</i></dt> 486 <dd>True if <i>file</i> is a version file.</dd> 487 </dl> 488 <p>The following option was not included because it was undocumented in most implementations, has been removed from some 489 implementations (including System V), and the functionality is provided by the shell (see <a href= 490 "../utilities/V3_chap02.html#tag_19_06_02"><i>2.6.2 Parameter Expansion</i></a> .</p> 491 <dl compact> 492 <dd></dd> 493 <dt><b>-l </b><i>string</i></dt> 494 <dd>The length of the string <i>string</i>.</dd> 495 </dl> 496 <p>The <b>-b</b>, <b>-c</b>, <b>-g</b>, <b>-p</b>, <b>-u</b>, and <b>-x</b> operands are derived from the SVID; historical BSD does 497 not provide them. The <b>-k</b> operand is derived from System V; historical BSD does not provide it.</p> 498 <p>On historical BSD systems, <i>test</i> <b>-w</b> <i>directory</i> always returned false because <i>test</i> tried to open the 499 directory for writing, which always fails.</p> 500 <p>Some additional primaries newly invented or from the KornShell appeared in an early proposal as part of the conditional command 501 (<b>[[]]</b>): <i>s1</i> <b>></b> <i>s2</i>, <i>s1</i> <b><</b> <i>s2</i>, <i>f1</i> <b>-nt</b> <i>f2</i>, <i>f1</i> 502 <b>-ot</b> <i>f2</i>, and <i>f1</i> <b>-ef</b> <i>f2</i>. They were not carried forward into the <i>test</i> utility when the 503 conditional command was removed from the shell because they had not been included in the <i>test</i> utility built into historical 504 implementations of the <a href="../utilities/sh.html"><i>sh</i></a> utility. However, they were later added to this standard once 505 support for them became widespread.</p> 506 <p>The <b>-t</b> <i>file_descriptor</i> primary is shown with a mandatory argument because the grammar is ambiguous if it can be 507 omitted. Historical implementations have allowed it to be omitted, providing a default of 1.</p> 508 <p>It is noted that <tt>'['</tt> is not part of the portable filename character set; however, since it is required to be encoded by 509 a single byte, and is part of the portable character set, the name of this utility forms a character string across all supported 510 locales.</p> 511 </blockquote> 512 <h4 class="mansect"><a name="tag_20_121_19" id="tag_20_121_19"></a>FUTURE DIRECTIONS</h4> 513 <blockquote> 514 <p>None.</p> 515 </blockquote> 516 <h4 class="mansect"><a name="tag_20_121_20" id="tag_20_121_20"></a>SEE ALSO</h4> 517 <blockquote> 518 <p><a href="../utilities/V3_chap01.html#tag_18_01_01_04"><i>1.1.1.4 File Read, Write, and Creation</i></a> , <a href= 519 "../utilities/find.html#"><i>find</i></a></p> 520 <p>XBD <a href="../basedefs/V1_chap08.html#tag_08"><i>8. Environment Variables</i></a> , <a href= 521 "../basedefs/V1_chap12.html#tag_12_02"><i>12.2 Utility Syntax Guidelines</i></a></p> 522 </blockquote> 523 <h4 class="mansect"><a name="tag_20_121_21" id="tag_20_121_21"></a>CHANGE HISTORY</h4> 524 <blockquote> 525 <p>First released in Issue 2.</p> 526 </blockquote> 527 <h4 class="mansect"><a name="tag_20_121_22" id="tag_20_121_22"></a>Issue 5</h4> 528 <blockquote> 529 <p>The FUTURE DIRECTIONS section is added.</p> 530 </blockquote> 531 <h4 class="mansect"><a name="tag_20_121_23" id="tag_20_121_23"></a>Issue 6</h4> 532 <blockquote> 533 <p>The <b>-h</b> operand is added for symbolic links, and access permission requirements are clarified for the <b>-r</b>, 534 <b>-w</b>, and <b>-x</b> operands to align with the IEEE P1003.2b draft standard.</p> 535 <p>The normative text is reworded to avoid use of the term "must" for application requirements.</p> 536 <p>The <b>-L</b> and <b>-S</b> operands are added for symbolic links and sockets.</p> 537 <p>IEEE Std 1003.1-2001/Cor 1-2002, item XCU/TC1/D6/38 is applied, adding XSI margin marking and shading to a line 538 in the OPERANDS section referring to the use of parentheses as arguments to the <i>test</i> utility.</p> 539 <p>IEEE Std 1003.1-2001/Cor 2-2004, item XCU/TC2/D6/30 is applied, rewording the existence primaries for the 540 <i>test</i> utility.</p> 541 </blockquote> 542 <h4 class="mansect"><a name="tag_20_121_24" id="tag_20_121_24"></a>Issue 7</h4> 543 <blockquote> 544 <p>Austin Group Interpretation 1003.1-2001 #107 is applied.</p> 545 <p>POSIX.1-2008, Technical Corrigendum 1, XCU/TC1-2008/0143 [291] is applied.</p> 546 <p>POSIX.1-2008, Technical Corrigendum 2, XCU/TC2-2008/0191 [898], XCU/TC2-2008/0192 [730], and XCU/TC2-2008/0193 [898] are 547 applied.</p> 548 </blockquote> 549 <h4 class="mansect"><a name="tag_20_121_25" id="tag_20_121_25"></a>Issue 8</h4> 550 <blockquote> 551 <p>Austin Group Defect 375 is applied, adding the <i>pathname1</i><b> -ef </b><i>pathname2</i>, 552 <i>pathname1</i><b> -nt </b><i>pathname2</i>, <i>pathname1</i><b> -ot </b><i>pathname2</i>, 553 <i>s1</i><b> > </b><i>s2</i>, and <i>s1</i><b> < </b><i>s2</i> primaries.</p> 554 <p>Austin Group Defect 1122 is applied, changing the description of <i>NLSPATH .</i></p> 555 <p>Austin Group Defect 1330 is applied, removing the obsolescent (and optional) <b>-a</b> and <b>-o</b> binary primaries, and 556 <tt>'('</tt> and <tt>')'</tt> operators.</p> 557 <p>Austin Group Defect 1348 is applied, removing "()" from "the <i>exec</i>() family of functions".</p> 558 <p>Austin Group Defect 1373 is applied, clarifying that when the utility name used is <i>[</i> the utility does not conform to 559 Guidelines 1 and 2.</p> 560 </blockquote> 561 <div class="box"><em>End of informative text.</em></div> 562 <hr> 563 <p> </p> 564 <a href="#top"><span class="topOfPage">return to top of page</span></a><br> 565 <hr size="2" noshade> 566 <center><font size="2">UNIX® is a registered Trademark of The Open Group.<br> 567 POSIX™ is a Trademark of The IEEE.<br> 568 Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved<br> 569 [ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href= 570 "../functions/contents.html">XSH</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../xrat/contents.html">XRAT</a> 571 ]</font></center> 572 <hr size="2" noshade> 573 <div class="NAVHEADER"> 574 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0"> 575 <tr class="nav"> 576 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/tee.html" accesskey="P"><<< 577 Previous</a></td> 578 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td> 579 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/time.html" accesskey="N">Next >>></a></td> 580 </tr> 581 </table> 582 <hr align="left" width="100%"></div> 583 </body> 584 </html>