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

📄 xargs.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<p>at the end of the line of the output from <b>-t</b>.</p></blockquote><h4><a name="tag_04_173_12"></a>OUTPUT FILES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_173_13"></a>EXTENDED DESCRIPTION</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_173_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt>&nbsp;&nbsp;&nbsp;&nbsp;0</dt><dd>All invocations of <i>utility</i> returned exit status zero.</dd><dt>1-125</dt><dd>A command line meeting the specified requirements could not be assembled, one or more of the invocations of <i>utility</i>returned a non-zero exit status, or some other error occurred.</dd><dt>&nbsp;&nbsp;126</dt><dd>The utility specified by <i>utility</i> was found but could not be invoked.</dd><dt>&nbsp;&nbsp;127</dt><dd>The utility specified by <i>utility</i> could not be found.</dd></dl></blockquote><h4><a name="tag_04_173_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>If a command line meeting the specified requirements cannot be assembled, the utility cannot be invoked, an invocation of theutility is terminated by a signal, or an invocation of the utility exits with exit status 255, the <i>xargs</i> utility shall writea diagnostic message and exit without processing any remaining input.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_173_16"></a>APPLICATION USAGE</h4><blockquote><p>The 255 exit status allows a utility being used by <i>xargs</i> to tell <i>xargs</i> to terminate if it knows no furtherinvocations using the current data stream will succeed. Thus, <i>utility</i> should explicitly <a href="../utilities/exit.html"><i>exit</i></a> with an appropriate value to avoid accidentally returning with 255.</p><p>Note that input is parsed as lines; &lt;blank&gt;s separate arguments. If <i>xargs</i> is used to bundle output of commands like<a href="../utilities/find.html"><i>find</i></a> <i>dir</i> <b>-print</b> or <a href="../utilities/ls.html"><i>ls</i></a> intocommands to be executed, unexpected results are likely if any filenames contain any &lt;blank&gt;s or &lt;newline&gt;s. This can befixed by using <a href="../utilities/find.html"><i>find</i></a> to call a script that converts each file found into a quoted stringthat is then piped to <i>xargs</i>. Note that the quoting rules used by <i>xargs</i> are not the same as in the shell. They werenot made consistent here because existing applications depend on the current rules and the shell syntax is not fully compatiblewith it. An easy rule that can be used to transform any string into a quoted form that <i>xargs</i> interprets correctly is toprecede each character in the string with a backslash.</p><p>On implementations with a large value for {ARG_MAX}, <i>xargs</i> may produce command lines longer than {LINE_MAX}. Forinvocation of utilities, this is not a problem. If <i>xargs</i> is being used to create a text file, users should explicitly setthe maximum command line length with the <b>-s</b> option.</p><p>The <a href="../utilities/command.html"><i>command</i></a>, <a href="../utilities/env.html"><i>env</i></a>, <a href="../utilities/nice.html"><i>nice</i></a>, <a href="../utilities/nohup.html"><i>nohup</i></a>, <a href="../utilities/time.html"><i>time</i></a>, and <i>xargs</i> utilities have been specified to use exit code 127 if an error occurs sothat applications can distinguish &quot;failure to find a utility&quot; from &quot;invoked utility exited with an error indication&quot;. The value127 was chosen because it is not commonly used for other meanings; most utilities use small values for &quot;normal error conditions''and the values above 128 can be confused with termination due to receipt of a signal. The value 126 was chosen in a similar mannerto indicate that the utility could be found, but not invoked. Some scripts produce meaningful error messages differentiating the126 and 127 cases. The distinction between exit codes 126 and 127 is based on KornShell practice that uses 127 when all attempts to<i>exec</i> the utility fail with [ENOENT], and uses 126 when any attempt to <i>exec</i> the utility fails for any otherreason.</p></blockquote><h4><a name="tag_04_173_17"></a>EXAMPLES</h4><blockquote><ol><li><p>The following command combines the output of the parenthesised commands onto one line, which is then written to the end-of-file<b>log</b>:</p><pre><tt>(logname; date; printf "%s\n" "$0 $*") | xargs &gt;&gt;log</tt></pre></li><li><p>The following command invokes <a href="../utilities/diff.html"><i>diff</i></a> with successive pairs of arguments originallytyped as command line arguments (assuming there are no embedded &lt;blank&gt;s in the elements of the original argument list):</p><pre><tt>printf "%s\n" "$*" | xargs -n 2 -x diff</tt></pre></li><li><p>In the following commands, the user is asked which files in the current directory are to be archived. The files are archivedinto <b>arch</b>; <i>a</i>, one at a time, or <i>b</i>, many at a time.</p><pre><tt>a. ls | xargs -p -L 1 ar -r arch<br>b. ls | xargs -p -L 1 | xargs ar -r arch</tt></pre></li><li><p>The following executes with successive pairs of arguments originally typed as command line arguments:</p><pre><tt>echo $* | xargs -n 2 diff</tt></pre></li><li><p>On XSI-conformant systems, the following moves all files from directory <b>$1</b> to directory <b>$2</b>, and echoes each movecommand just before doing it:</p><pre><tt>ls $1 | xargs -I {} -t mv $1/{} $2/{}</tt></pre></li></ol></blockquote><h4><a name="tag_04_173_18"></a>RATIONALE</h4><blockquote><p>The <i>xargs</i> utility was usually found only in System V-based systems; BSD systems included an <i>apply</i> utility thatprovided functionality similar to <i>xargs</i> <b>-n</b> <i>number</i>. The SVID lists <i>xargs</i> as a software developmentextension. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 does not share the view that it is used only for development, andtherefore it is not optional.</p><p>The classic application of the <i>xargs</i> utility is in conjunction with the <a href="../utilities/find.html"><i>find</i></a>utility to reduce the number of processes launched by a simplistic use of the <a href="../utilities/find.html"><i>find</i></a><b>-exec</b> combination. The <i>xargs</i> utility is also used to enforce an upper limit on memory required to launch a process.With this basis in mind, this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 selected only the minimal features required.</p><p>Although the 255 exit status is mostly an accident of historical implementations, it allows a utility being used by <i>xargs</i>to tell <i>xargs</i> to terminate if it knows no further invocations using the current data stream shall succeed. Any non-zero exitstatus from a utility falls into the 1-125 range when <i>xargs</i> exits. There is no statement of how the various non-zero utilityexit status codes are accumulated by <i>xargs</i>. The value could be the addition of all codes, their highest value, the last onereceived, or a single value such as 1. Since no algorithm is arguably better than the others, and since many of the standardutilities say little more (portably) than &quot;pass/fail&quot;, no new algorithm was invented.</p><p>Several other <i>xargs</i> options were withdrawn because simple alternatives already exist within this volume ofIEEE&nbsp;Std&nbsp;1003.1-2001. For example, the <b>-i</b> <i>replstr</i> option can be just as efficiently performed using a shell<b>for</b> loop. Since <i>xargs</i> calls an <i>exec</i> function with each input line, the <b>-i</b> option does not usuallyexploit the grouping capabilities of <i>xargs</i>.</p><p>The requirement that <i>xargs</i> never produces command lines such that invocation of <i>utility</i> is within 2048 bytes ofhitting the POSIX <i>exec</i> {ARG_MAX} limitations is intended to guarantee that the invoked utility has room to modify itsenvironment variables and command line arguments and still be able to invoke another utility. Note that the minimum {ARG_MAX}allowed by the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 is 4096 bytes and the minimum value allowed by thisvolume of IEEE&nbsp;Std&nbsp;1003.1-2001 is 2048 bytes; therefore, the 2048 bytes difference seems reasonable. Note, however, that<i>xargs</i> may never be able to invoke a utility if the environment passed in to <i>xargs</i> comes close to using {ARG_MAX}bytes.</p><p>The version of <i>xargs</i> required by this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 is required to wait for the completion ofthe invoked command before invoking another command. This was done because historical scripts using <i>xargs</i> assumed sequentialexecution. Implementations wanting to provide parallel operation of the invoked utilities are encouraged to add an option enablingparallel invocation, but should still wait for termination of all of the children before <i>xargs</i> terminates normally.</p><p>The <b>-e</b> option was omitted from the ISO&nbsp;POSIX-2:1993 standard in the belief that the <i>eofstr</i> option-argumentwas recognized only when it was on a line by itself and before quote and escape processing were performed, and that the logicalend-of-file processing was only enabled if a <b>-e</b> option was specified. In that case, a simple <a href="../utilities/sed.html"><i>sed</i></a> script could be used to duplicate the <b>-e</b> functionality. Further investigationrevealed that:</p><ul><li><p>The logical end-of-file string was checked for after quote and escape processing, making a <a href="../utilities/sed.html"><i>sed</i></a> script that provided equivalent functionality much more difficult to write.</p></li><li><p>The default was to perform logical end-of-file processing with an underscore as the logical end-of-file string.</p></li></ul><p>To correct this misunderstanding, the <b>-E</b> <i>eofstr</i> option was adopted from the X/Open Portability Guide. Users shouldnote that the description of the <b>-E</b> option matches historical documentation of the <b>-e</b> option (which was not adoptedbecause it did not support the Utility Syntax Guidelines), by saying that if <i>eofstr</i> is the null string, logical end-of-fileprocessing is disabled. Historical implementations of <i>xargs</i> actually did not disable logical end-of-file processing; theytreated a null argument found in the input as a logical end-of-file string. (A null <i>string</i> argument could be generated usingsingle or double quotes ( <tt>''</tt> or <tt>""</tt> ). Since this behavior was not documented historically, it is considered to bea bug.</p></blockquote><h4><a name="tag_04_173_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_173_20"></a>SEE ALSO</h4><blockquote><p><a href="xcu_chap02.html#tag_02"><i>Shell Command Language</i></a> , <a href="echo.html"><i>echo</i></a> , <a href="find.html"><i>find</i></a> , the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <i>exec</i></p></blockquote><h4><a name="tag_04_173_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><h4><a name="tag_04_173_22"></a>Issue 5</h4><blockquote><p>A second FUTURE DIRECTION is added.</p></blockquote><h4><a name="tag_04_173_23"></a>Issue 6</h4><blockquote><p>The obsolescent <b>-e</b>, <b>-i</b>, and <b>-l</b> options are removed.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The <b>-p</b> option is added.</p></li><li><p>In the INPUT FILES section, the file <b>/dev/tty</b> is used to read responses required by the <b>-p</b> option.</p></li><li><p>The STDERR section is updated to describe the <b>-p</b> option.</p></li></ul><p>The description of the <b>-E</b> option is aligned with the ISO&nbsp;POSIX-2:1993 standard.</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 + -