801 lines
25 KiB
HTML
801 lines
25 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>test</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="test"></a> <a name="tag_04_140"></a><!-- test -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_04_140_01"></a>NAME</h4>
|
|
|
|
<blockquote>test - evaluate expression</blockquote>
|
|
|
|
<h4><a name="tag_04_140_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>test</tt> <b>[</b><i>expression</i><b>]</b><tt><br>
|
|
<br>
|
|
[</tt> <b>[</b><i>expression</i><b>]</b> <tt>]<br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>test</i> utility shall evaluate the <i>expression</i> and indicate the result of the evaluation by its exit status. An
|
|
exit status of zero indicates that the expression evaluated as true and an exit status of 1 indicates that the expression evaluated
|
|
as false.</p>
|
|
|
|
<p>In the second form of the utility, which uses <tt>"[]"</tt> rather than <i>test</i>, the application shall ensure that the
|
|
square brackets are separate arguments.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_04"></a>OPTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>test</i> utility shall not recognize the <tt>"--"</tt> argument in the manner specified by guideline 10 in the Base
|
|
Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax
|
|
Guidelines</a>.</p>
|
|
|
|
<p>No options shall be supported.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_05"></a>OPERANDS</h4>
|
|
|
|
<blockquote>
|
|
<p>The application shall ensure that all operators and elements of primaries are presented as separate arguments to the <i>test</i>
|
|
utility.</p>
|
|
|
|
<p>The following primaries can be used to construct <i>expression</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-b </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a block special file.</dd>
|
|
|
|
<dt><b>-c </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a character special file.</dd>
|
|
|
|
<dt><b>-d </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a directory.</dd>
|
|
|
|
<dt><b>-e </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists.</dd>
|
|
|
|
<dt><b>-f </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a regular file.</dd>
|
|
|
|
<dt><b>-g </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and its set-group-ID flag is set.</dd>
|
|
|
|
<dt><b>-h </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a symbolic link.</dd>
|
|
|
|
<dt><b>-L </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a symbolic link.</dd>
|
|
|
|
<dt><b>-n </b> <i>string</i></dt>
|
|
|
|
<dd>True if the length of <i>string</i> is non-zero.</dd>
|
|
|
|
<dt><b>-p </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> is a FIFO.</dd>
|
|
|
|
<dt><b>-r </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is readable. True shall indicate that permission to read from <i>file</i> will be granted, as
|
|
defined in <a href="xcu_chap01.html#tag_01_07_01_04"><i>File Read, Write, and Creation</i></a> .</dd>
|
|
|
|
<dt><b>-S </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is a socket.</dd>
|
|
|
|
<dt><b>-s </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and has a size greater than zero.</dd>
|
|
|
|
<dt><b>-t </b> <i>file_descriptor</i></dt>
|
|
|
|
<dd><br>
|
|
True if the file whose file descriptor number is <i>file_descriptor</i> is open and is associated with a terminal.</dd>
|
|
|
|
<dt><b>-u </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and its set-user-ID flag is set.</dd>
|
|
|
|
<dt><b>-w </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is writable. True shall indicate that permission to write from <i>file</i> will be granted, as
|
|
defined in <a href="xcu_chap01.html#tag_01_07_01_04"><i>File Read, Write, and Creation</i></a> .</dd>
|
|
|
|
<dt><b>-x </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is executable. True shall indicate that permission to execute <i>file</i> will be granted, as
|
|
defined in <a href="xcu_chap01.html#tag_01_07_01_04"><i>File Read, Write, and Creation</i></a> . If <i>file</i> is a directory,
|
|
true shall indicate that permission to search <i>file</i> will be granted.</dd>
|
|
|
|
<dt><b>-z </b> <i>string</i></dt>
|
|
|
|
<dd>True if the length of string <i>string</i> is zero.</dd>
|
|
|
|
<dt><i>string</i></dt>
|
|
|
|
<dd>True if the string <i>string</i> is not the null string.</dd>
|
|
|
|
<dt><i>s1</i><b> = </b> <i>s2</i></dt>
|
|
|
|
<dd>True if the strings <i>s1</i> and <i>s2</i> are identical.</dd>
|
|
|
|
<dt><i>s1</i><b> != </b> <i>s2</i></dt>
|
|
|
|
<dd>True if the strings <i>s1</i> and <i>s2</i> are not identical.</dd>
|
|
|
|
<dt><i>n1</i><b> -eq </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integers <i>n1</i> and <i>n2</i> are algebraically equal.</dd>
|
|
|
|
<dt><i>n1</i><b> -ne </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integers <i>n1</i> and <i>n2</i> are not algebraically equal.</dd>
|
|
|
|
<dt><i>n1</i><b> -gt </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integer <i>n1</i> is algebraically greater than the integer <i>n2</i>.</dd>
|
|
|
|
<dt><i>n1</i><b> -ge </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integer <i>n1</i> is algebraically greater than or equal to the integer <i>n2</i>.</dd>
|
|
|
|
<dt><i>n1</i><b> -lt </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integer <i>n1</i> is algebraically less than the integer <i>n2</i>.</dd>
|
|
|
|
<dt><i>n1</i><b> -le </b> <i>n2</i></dt>
|
|
|
|
<dd>True if the integer <i>n1</i> is algebraically less than or equal to the integer <i>n2</i>.</dd>
|
|
|
|
<dt><i>expression1</i><b> -a </b> <i>expression2</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
|
|
"0"><br>
|
|
True if both <i>expression1</i> and <i>expression2</i> are true. The <b>-a</b> binary primary is left associative. It has a higher
|
|
precedence than <b>-o</b>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
|
|
|
|
<dt><i>expression1</i><b> -o </b> <i>expression2</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
|
|
"0"><br>
|
|
True if either <i>expression1</i> or <i>expression2</i> is true. The <b>-o</b> binary primary is left associative. <img src=
|
|
"../images/opt-end.gif" alt="[Option End]" border="0"></dd>
|
|
</dl>
|
|
|
|
<p>With the exception of the <b>-h</b> <i>file</i> and <b>-L</b> <i>file</i> primaries, if a <i>file</i> argument is a symbolic
|
|
link, <i>test</i> shall evaluate the expression by resolving the symbolic link and using the file referenced by the link.</p>
|
|
|
|
<p>These primaries can be combined with the following operators:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>! </b> <i>expression</i></dt>
|
|
|
|
<dd>True if <i>expression</i> is false.</dd>
|
|
|
|
<dt><b>( </b> <i>expression </i> <b>)</b></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
True if <i>expression</i> is true. The parentheses can be used to alter the normal precedence and associativity. <img src=
|
|
"../images/opt-end.gif" alt="[Option End]" border="0"></dd>
|
|
</dl>
|
|
|
|
<p>The primaries with two elements of the form:</p>
|
|
|
|
<pre>
|
|
<tt>-</tt><i>primary_operator primary_operand</i>
|
|
</pre>
|
|
|
|
<p>are known as <i>unary primaries</i>. The primaries with three elements in either of the two forms:</p>
|
|
|
|
<pre>
|
|
<i>primary_operand</i> <tt>-</tt><i>primary_operator primary_operand
|
|
<br>
|
|
primary_operand primary_operator primary_operand</i>
|
|
</pre>
|
|
|
|
<p>are known as <i>binary primaries</i>. Additional implementation-defined operators and <i>primary_operator</i>s may be provided
|
|
by implementations. They shall be of the form - <i>operator</i> where the first character of <i>operator</i> is not a digit.</p>
|
|
|
|
<p>The algorithm for determining the precedence of the operators and the return value that shall be generated is based on the
|
|
number of arguments presented to <i>test</i>. (However, when using the <tt>"[...]"</tt> form, the right-bracket final argument
|
|
shall not be counted in this algorithm.)</p>
|
|
|
|
<p>In the following list, $1, $2, $3, and $4 represent the arguments presented to <i>test</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt>0 arguments:</dt>
|
|
|
|
<dd>Exit false (1).</dd>
|
|
|
|
<dt>1 argument:</dt>
|
|
|
|
<dd>Exit true (0) if $1 is not null; otherwise, exit false.</dd>
|
|
|
|
<dt>2 arguments:</dt>
|
|
|
|
<dd>
|
|
<ul>
|
|
<li>
|
|
<p>If $1 is <tt>'!'</tt> , exit true if $2 is null, false if $2 is not null.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If $1 is a unary primary, exit true if the unary test is true, false if the unary test is false.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Otherwise, produce unspecified results.</p>
|
|
</li>
|
|
</ul>
|
|
</dd>
|
|
|
|
<dt>3 arguments:</dt>
|
|
|
|
<dd>
|
|
<ul>
|
|
<li>
|
|
<p>If $2 is a binary primary, perform the binary test of $1 and $3.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If $1 is <tt>'!'</tt> , negate the two-argument test of $2 and $3.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If $1 is <tt>'('</tt> and $3 is <tt>')'</tt> , perform the unary test of $2.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Otherwise, produce unspecified results.</p>
|
|
</li>
|
|
</ul>
|
|
</dd>
|
|
|
|
<dt>4 arguments:</dt>
|
|
|
|
<dd>
|
|
<ul>
|
|
<li>
|
|
<p>If $1 is <tt>'!'</tt> , negate the three-argument test of $2, $3, and $4.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
If $1 is <tt>'('</tt> and $4 is <tt>')'</tt> , perform the two-argument test of $2 and $3. <img src="../images/opt-end.gif" alt=
|
|
"[Option End]" border="0"></p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Otherwise, the results are unspecified.</p>
|
|
</li>
|
|
</ul>
|
|
</dd>
|
|
|
|
<dt>>4 arguments:</dt>
|
|
|
|
<dd>The results are unspecified.
|
|
|
|
<p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
On XSI-conformant systems, combinations of primaries and operators shall be evaluated using the precedence and associativity rules
|
|
described previously. In addition, the string comparison binary primaries <tt>'='</tt> and <tt>"!="</tt> shall have a higher
|
|
precedence than any unary primary. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_06"></a>STDIN</h4>
|
|
|
|
<blockquote>
|
|
<p>Not used.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_07"></a>INPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_08"></a>ENVIRONMENT VARIABLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The following environment variables shall affect the execution of <i>test</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>LANG</i></dt>
|
|
|
|
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
|
|
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
|
|
|
|
<dt><i>LC_ALL</i></dt>
|
|
|
|
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
|
|
|
|
<dt><i>LC_CTYPE</i></dt>
|
|
|
|
<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
|
|
opposed to multi-byte characters in arguments).</dd>
|
|
|
|
<dt><i>LC_MESSAGES</i></dt>
|
|
|
|
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
|
|
error.</dd>
|
|
|
|
<dt><i>NLSPATH</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
|
|
"[Option End]" border="0"></dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_09"></a>ASYNCHRONOUS EVENTS</h4>
|
|
|
|
<blockquote>
|
|
<p>Default.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_10"></a>STDOUT</h4>
|
|
|
|
<blockquote>
|
|
<p>Not used.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_11"></a>STDERR</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard error shall be used only for diagnostic messages.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_12"></a>OUTPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_13"></a>EXTENDED DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_14"></a>EXIT STATUS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following exit values shall be returned:</p>
|
|
|
|
<dl compact>
|
|
<dt> 0</dt>
|
|
|
|
<dd><i>expression</i> evaluated to true.</dd>
|
|
|
|
<dt> 1</dt>
|
|
|
|
<dd><i>expression</i> evaluated to false or <i>expression</i> was missing.</dd>
|
|
|
|
<dt>>1</dt>
|
|
|
|
<dd>An error occurred.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_15"></a>CONSEQUENCES OF ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>Default.</p>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_04_140_16"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>Scripts should be careful when dealing with user-supplied input that could be confused with primaries and operators. Unless the
|
|
application writer knows all the cases that produce input to the script, invocations like:</p>
|
|
|
|
<pre>
|
|
<tt>test "$1" -a "$2"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>should be written as:</p>
|
|
|
|
<pre>
|
|
<tt>test "$1" && test "$2"
|
|
</tt>
|
|
</pre>
|
|
|
|
<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, in cases
|
|
where maximal portability is of concern, replace:</p>
|
|
|
|
<pre>
|
|
<tt>test expr1 -a expr2
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>with:</p>
|
|
|
|
<pre>
|
|
<tt>test expr1 && test expr2
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>and replace:</p>
|
|
|
|
<pre>
|
|
<tt>test expr1 -o expr2
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>with:</p>
|
|
|
|
<pre>
|
|
<tt>test expr1 || test expr2
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>but note that, in <i>test</i>, <b>-a</b> has higher precedence than <b>-o</b> while <tt>"&&"</tt> and <tt>"||"</tt> have
|
|
equal precedence in the shell.</p>
|
|
|
|
<p>Parentheses or braces can be used in the shell command language to effect grouping.</p>
|
|
|
|
<p>Parentheses must be escaped when using <a href="../utilities/sh.html"><i>sh</i></a>; for example:</p>
|
|
|
|
<pre>
|
|
<tt>test \( expr1 -a expr2 \) -o expr3
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>This command is not always portable outside XSI-conformant systems. The following form can be used instead:</p>
|
|
|
|
<pre>
|
|
<tt>( test expr1 && test expr2 ) || test expr3
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The two commands:</p>
|
|
|
|
<pre>
|
|
<tt>test "$1"
|
|
test ! "$1"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>could not be used reliably on some historical systems. Unexpected results would occur if such a <i>string</i> expression were
|
|
used and $1 expanded to <tt>'!'</tt> , <tt>'('</tt> , or a known unary primary. Better constructs are:</p>
|
|
|
|
<pre>
|
|
<tt>test -n "$1"
|
|
test -z "$1"
|
|
</tt>
|
|
</pre>
|
|
|
|
respectively.
|
|
|
|
<p>Historical systems have also been unreliable given the common construct:</p>
|
|
|
|
<pre>
|
|
<tt>test "$response" = "expected string"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>One of the following is a more reliable form:</p>
|
|
|
|
<pre>
|
|
<tt>test "X$response" = "Xexpected string"
|
|
test "expected string" = "$response"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Note that the second form assumes that <i>expected string</i> could not be confused with any unary primary. If <i>expected
|
|
string</i> starts with <tt>'-'</tt> , <tt>'('</tt> , <tt>'!'</tt> , or even <tt>'='</tt> , the first form should be used instead.
|
|
Using the preceding rules without the XSI marked extensions, any of the three comparison forms is reliable, given any input.
|
|
(However, note that the strings are quoted in all cases.)</p>
|
|
|
|
<p>Because the string comparison binary primaries, <tt>'='</tt> and <tt>"!="</tt> , have a higher precedence than any unary primary
|
|
in the greater than 4 argument case, unexpected results can occur if arguments are not properly prepared. For example, in:</p>
|
|
|
|
<pre>
|
|
<tt>test -d $1 -o -d $2
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>If $1 evaluates to a possible directory name of <tt>'='</tt> , the first three arguments are considered a string comparison,
|
|
which shall cause a syntax error when the second <b>-d</b> is encountered. One of the following forms prevents this; the second is
|
|
preferred:</p>
|
|
|
|
<pre>
|
|
<tt>test \( -d "$1" \) -o \( -d "$2" \)
|
|
test -d "$1" || test -d "$2"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Also in the greater than 4 argument case:</p>
|
|
|
|
<pre>
|
|
<tt>test "$1" = "bat" -a "$2" = "ball"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>syntax errors occur if $1 evaluates to <tt>'('</tt> or <tt>'!'</tt> . One of the following forms prevents this; the third is
|
|
preferred:</p>
|
|
|
|
<pre>
|
|
<tt>test "X$1" = "Xbat" -a "X$2" = "Xball"
|
|
test "$1" = "bat" && test "$2" = "ball"
|
|
test "X$1" = "Xbat" && test "X$2" = "Xball"
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_17"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<ol>
|
|
<li>
|
|
<p>Exit if there are not two or three arguments (two variations):</p>
|
|
|
|
<pre>
|
|
<tt>if [ $# -ne 2 -a $# -ne 3 ]; then exit 1; fi
|
|
if [ $# -lt 2 -o $# -gt 3 ]; then exit 1; fi
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Perform a <a href="../utilities/mkdir.html"><i>mkdir</i></a> if a directory does not exist:</p>
|
|
|
|
<pre>
|
|
<tt>test ! -d tempdir && mkdir tempdir
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Wait for a file to become non-readable:</p>
|
|
|
|
<pre>
|
|
<tt>while test -r thefile
|
|
do
|
|
sleep 30
|
|
done
|
|
echo '"thefile" is no longer readable'
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Perform a command if the argument is one of three strings (two variations):</p>
|
|
|
|
<pre>
|
|
<tt>if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ]
|
|
then
|
|
</tt> <i>command</i><tt>fi
|
|
<br>
|
|
case "$1" in
|
|
pear|grape|apple)</tt> <i>command</i> <tt>;;
|
|
esac
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
</ol>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_18"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>The KornShell-derived conditional command (double bracket <b>[[]]</b>) was removed from the shell command language description
|
|
in an early proposal. Objections were raised that the real problem is misuse of the <i>test</i> command ( <b>[</b>), and putting it
|
|
into the shell is the wrong way to fix the problem. Instead, proper documentation and a new shell reserved word ( <b>!</b>) are
|
|
sufficient.</p>
|
|
|
|
<p>Tests that require multiple <i>test</i> operations can be done at the shell level using individual invocations of the
|
|
<i>test</i> command and shell logicals, rather than using the error-prone <b>-o</b> flag of <i>test</i>.</p>
|
|
|
|
<p>XSI-conformant systems support more than four arguments.</p>
|
|
|
|
<p>XSI-conformant systems support the combining of primaries with the following constructs:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>expression1</i> <b>-a</b> <i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
True if both <i>expression1</i> and <i>expression2</i> are true.</dd>
|
|
|
|
<dt><i>expression1</i> <b>-o</b> <i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
True if at least one of <i>expression1</i> and <i>expression2</i> are true.</dd>
|
|
|
|
<dt><b>(</b> <i>expression</i> <b>)</b></dt>
|
|
|
|
<dd><br>
|
|
True if <i>expression</i> is true.</dd>
|
|
</dl>
|
|
|
|
<p>In evaluating these more complex combined expressions, the following precedence rules are used:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The unary primaries have higher precedence than the algebraic binary primaries.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The unary primaries have lower precedence than the string binary primaries.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The unary and binary primaries have higher precedence than the unary <i>string</i> primary.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <b>!</b> operator has higher precedence than the <b>-a</b> operator, and the <b>-a</b> operator has higher precedence than
|
|
the <b>-o</b> operator.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <b>-a</b> and <b>-o</b> operators are left associative.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The parentheses can be used to alter the normal precedence and associativity.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The BSD and System V versions of <b>-f</b> are not the same. The BSD definition was:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-f </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and is not a directory.</dd>
|
|
</dl>
|
|
|
|
<p>The SVID version (true if the file exists and is a regular file) was chosen for this volume of IEEE Std 1003.1-2001
|
|
because its use is 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>
|
|
|
|
<p>The <b>-e</b> primary, possessing similar functionality to that provided by the C shell, was added because it provides the only
|
|
way for a shell script to find out if a file exists without trying to open the file. Since implementations are allowed to add
|
|
additional file types, a portable script cannot use:</p>
|
|
|
|
<pre>
|
|
<tt>test -b foo -o -c foo -o -d foo -o -f foo -o -p foo
|
|
</tt>
|
|
</pre>
|
|
|
|
<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>
|
|
|
|
<pre>
|
|
<tt>test -f foo -o -d foo
|
|
</tt>
|
|
</pre>
|
|
|
|
<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>
|
|
primary (with the same meaning), but this was changed to <b>-e</b> because there were concerns about the high probability of humans
|
|
confusing the <b>-a</b> primary with the <b>-a</b> binary operator.</p>
|
|
|
|
<p>The following options were not included in this volume of IEEE Std 1003.1-2001, although they are provided by some
|
|
implementations. These operands should not be used by new implementations for other purposes:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-k </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> exists and its sticky bit is set.</dd>
|
|
|
|
<dt><b>-C </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> is a contiguous file.</dd>
|
|
|
|
<dt><b>-V </b> <i>file</i></dt>
|
|
|
|
<dd>True if <i>file</i> is a version file.</dd>
|
|
</dl>
|
|
|
|
<p>The following option was not included because it was undocumented in most implementations, has been removed from some
|
|
implementations (including System V), and the functionality is provided by the shell (see <a href=
|
|
"xcu_chap02.html#tag_02_06_02"><i>Parameter Expansion</i></a> .</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-l </b> <i>string</i></dt>
|
|
|
|
<dd>The length of the string <i>string</i>.</dd>
|
|
</dl>
|
|
|
|
<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
|
|
not provide them. The <b>-k</b> operand is derived from System V; historical BSD does not provide it.</p>
|
|
|
|
<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
|
|
directory for writing, which always fails.</p>
|
|
|
|
<p>Some additional primaries newly invented or from the KornShell appeared in an early proposal as part of the conditional command
|
|
( <b>[[]]</b>): <i>s1</i> <b>></b> <i>s2</i>, <i>s1</i> <b><</b> <i>s2</i>, <i>str</i> <b>=</b> <i>pattern</i>, <i>str</i>
|
|
<b>!=</b> <i>pattern</i>, <i>f1</i> <b>-nt</b> <i>f2</i>, <i>f1</i> <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 conditional command was removed from the shell because they have not
|
|
been included in the <i>test</i> utility built into historical implementations of the <a href="../utilities/sh.html"><i>sh</i></a>
|
|
utility.</p>
|
|
|
|
<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
|
|
omitted. Historical implementations have allowed it to be omitted, providing a default of 1.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_19"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_20"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="xcu_chap01.html#tag_01_07_01_04"><i>File Read, Write, and Creation</i></a> , <a href="find.html"><i>find</i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_21"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 2.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_22"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>The FUTURE DIRECTIONS section is added.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_140_23"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The <b>-h</b> operand is added for symbolic links, and access permission requirements are clarified for the <b>-r</b>,
|
|
<b>-w</b>, and <b>-x</b> operands to align with the IEEE P1003.2b draft standard.</p>
|
|
|
|
<p>The normative text is reworded to avoid use of the term "must" for application requirements.</p>
|
|
|
|
<p>The <b>-L</b> and <b>-S</b> operands are added for symbolic links and sockets.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|