⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expr.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<p class="tent">Matching expression; see below.</p></td></tr><tr valign="top"><td align="left"><p class="tent">(&nbsp;<i>expr</i>&nbsp;)</p></td><td align="left"><p class="tent">Grouping symbols. Any expression can be placed within parentheses. Parentheses can be nested to a depth of{EXPR_NEST_MAX}.</p></td></tr><tr valign="top"><td align="left"><p class="tent"><i>integer</i></p></td><td align="left"><p class="tent">An argument consisting only of an (optional) unary minus followed by digits.</p></td></tr><tr valign="top"><td align="left"><p class="tent"><i>string</i></p></td><td align="left"><p class="tent">A string argument; see below.</p></td></tr></table></center><h5><a name="tag_04_50_13_01"></a>Matching Expression</h5><p>The <tt>':'</tt> matching operator shall compare the string resulting from the evaluation of <i>expr1</i> with the regularexpression pattern resulting from the evaluation of <i>expr2</i>. Regular expression syntax shall be that defined in the BaseDefinitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap09.html#tag_09_03">Section 9.3, Basic RegularExpressions</a>, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at thefirst character of a string are matched by the regular expression) and, therefore, it is unspecified whether <tt>'^'</tt> is aspecial character in that context. Usually, the matching operator shall return a string representing the number of charactersmatched ( <tt>'0'</tt> on failure). Alternatively, if the pattern contains at least one regular expression subexpression<tt>"[\(...\)]"</tt> , the string corresponding to <tt>"\1"</tt> shall be returned.</p><h5><a name="tag_04_50_13_02"></a>String Operand</h5><p>A string argument is an argument that cannot be identified as an <i>integer</i> argument or as one of the expression operatorsymbols shown in the OPERANDS section.</p><p>The use of string arguments <b>length</b>, <b>substr</b>, <b>index</b>, or <b>match</b> produces unspecified results.</p></blockquote><h4><a name="tag_04_50_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt>&nbsp;0</dt><dd>The <i>expression</i> evaluates to neither null nor zero.</dd><dt>&nbsp;1</dt><dd>The <i>expression</i> evaluates to null or zero.</dd><dt>&nbsp;2</dt><dd>Invalid <i>expression</i>.</dd><dt>&gt;2</dt><dd>An error occurred.</dd></dl></blockquote><h4><a name="tag_04_50_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_50_16"></a>APPLICATION USAGE</h4><blockquote><p>After argument processing by the shell, <i>expr</i> is not required to be able to tell the difference between an operator and anoperand except by the value. If <tt>"$a"</tt> is <tt>'='</tt> , the command:</p><pre><tt>expr $a = '='</tt></pre><p>looks like:</p><pre><tt>expr = = =</tt></pre><p>as the arguments are passed to <i>expr</i> (and they all may be taken as the <tt>'='</tt> operator). The following worksreliably:</p><pre><tt>expr X$a = X=</tt></pre><p>Also note that this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 permits implementations to extend utilities. The <i>expr</i>utility permits the integer arguments to be preceded with a unary minus. This means that an integer argument could look like anoption. Therefore, the conforming application must employ the <tt>"--"</tt> construct of Guideline 10 of the Base Definitionsvolume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility SyntaxGuidelines</a> to protect its operands if there is any chance the first operand might be a negative integer (or any string with aleading minus).</p></blockquote><h4><a name="tag_04_50_17"></a>EXAMPLES</h4><blockquote><p>The <i>expr</i> utility has a rather difficult syntax:</p><ul><li><p>Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line.</p></li><li><p>Each part of the expression is composed of separate arguments, so liberal usage of &lt;blank&gt;s is required. For example:</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b>Invalid</b></p></th><th align="center"><p class="tent"><b>Valid</b></p></th></tr><tr valign="top"><td align="left"><p class="tent"><i>expr</i> 1+2</p></td><td align="left"><p class="tent"><i>expr</i> 1 + 2</p></td></tr><tr valign="top"><td align="left"><p class="tent"><i>expr</i> "1 + 2"</p></td><td align="left"><p class="tent"><i>expr</i> 1 + 2</p></td></tr><tr valign="top"><td align="left"><p class="tent"><i>expr</i> 1 + (2 * 3)</p></td><td align="left"><p class="tent"><i>expr</i> 1 + \( 2 \* 3 \)</p></td></tr></table></center></li></ul><p>In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than theirequivalents in <i>expr</i>. Newly written scripts should avoid <i>expr</i> in favor of the new features within the shell; see <ahref="xcu_chap02.html#tag_02_05"><i>Parameters and Variables</i></a> and <a href="xcu_chap02.html#tag_02_06_04"><i>ArithmeticExpansion</i></a> .</p><p>The following command:</p><pre><tt>a=$(expr $a + 1)</tt></pre><p>adds 1 to the variable <i>a</i>.</p><p>The following command, for <tt>"$a"</tt> equal to either <b>/usr/abc/file</b> or just <b>file</b>:</p><pre><tt>expr $a : '.*/\(.*\)' \| $a</tt></pre><p>returns the last segment of a pathname (that is, <b>file</b>). Applications should avoid the character <tt>'/'</tt> used aloneas an argument; <i>expr</i> may interpret it as the division operator.</p><p>The following command:</p><pre><tt>expr "//$a" : '.*/\(.*\)'</tt></pre><p>is a better representation of the previous example. The addition of the <tt>"//"</tt> characters eliminates any ambiguity aboutthe division operator and simplifies the whole expression. Also note that pathnames may contain characters contained in the<i>IFS</i> variable and should be quoted to avoid having <tt>"$a"</tt> expand into multiple arguments.</p><p>The following command:</p><pre><tt>expr "$VAR" : '.*'</tt></pre><p>returns the number of characters in <i>VAR</i>.</p></blockquote><h4><a name="tag_04_50_18"></a>RATIONALE</h4><blockquote><p>In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historicalapplications.</p><p>The use of a leading circumflex in the BRE is unspecified because many historical implementations have treated it as a specialcharacter, despite their system documentation. For example:</p><pre><tt>expr foo : ^foo     expr ^foo : ^foo</tt></pre><p>return 3 and 0, respectively, on those systems; their documentation would imply the reverse. Thus, the anchoring condition isleft unspecified to avoid breaking historical scripts relying on this undocumented feature.</p></blockquote><h4><a name="tag_04_50_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_50_20"></a>SEE ALSO</h4><blockquote><p><a href="xcu_chap02.html#tag_02_05"><i>Parameters and Variables</i></a> , <a href="xcu_chap02.html#tag_02_06_04"><i>ArithmeticExpansion</i></a></p></blockquote><h4><a name="tag_04_50_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><h4><a name="tag_04_50_22"></a>Issue 5</h4><blockquote><p>The FUTURE DIRECTIONS section is added.</p></blockquote><h4><a name="tag_04_50_23"></a>Issue 6</h4><blockquote><p>The <i>expr</i> utility is aligned with the IEEE&nbsp;P1003.2b draft standard, to include resolution of IEEE PASC Interpretation1003.2 #104.</p><p>The normative text is reworded to avoid use of the term &quot;must&quot; for application requirements.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -