📄 xargs.html
字号:
If the<i>utility</i>operand names any of the special built-in utilities in<xref href=sbi><a href="chap2.html#tag_001_014">Special Built-in Utilities</a></xref>,the results are undefined.<dt><i>argument</i><dd>An initial option or operand for the invocation of<i>utility</i>.</dl></blockquote><h4><a name = "tag_001_014_2928"> </a>STDIN</h4><blockquote>The standard input must be a text file.The results are unspecified if an end-of-filecondition is detected immediately following an escapednewline character.</blockquote><h4><a name = "tag_001_014_2929"> </a>INPUT FILES</h4><blockquote>The file<b>/dev/tty</b>is used to read responses required by the<b>-p</b>option.</blockquote><h4><a name = "tag_001_014_2930"> </a>ENVIRONMENT VARIABLES</h4><blockquote>The following environment variables affect the execution of<i>xargs</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_COLLATE</i><dd>Determine the locale for thebehaviour of ranges, equivalence classesand multi-character collating elementsused in theextended regular expression defined for the<b>yesexpr</b>locale keyword in the LC_MESSAGES category.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text data ascharacters (for example, single- as opposed to multi-byte charactersin arguments and input files) andthe behaviour of character classesused in the extended regular expression defined for the<b>yesexpr</b>locale keyword in the LC_MESSAGES category.<dt><i>LC_MESSAGES</i><dd>Determine the locale for the processing of affirmative responsesand that should be used to affectthe format and contents of diagnosticmessages written to standard error.<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i><dt><i>PATH</i><dd>Determine the location of<i>utility</i>,as described inthe <b>XBD</b> specification, <a href="../xbd/envvar.html"><b>Environment Variables</b> </a> .</dl></blockquote><h4><a name = "tag_001_014_2931"> </a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_2932"> </a>STDOUT</h4><blockquote>Not used.<br></blockquote><h4><a name = "tag_001_014_2933"> </a>STDERR</h4><blockquote>Used for diagnostic messages and the<b>-t</b>and<b>-p</b>options.If the<b>-t</b>option is specified, the<i>utility</i>and its constructed argument list will be written to standard error,as it will beinvoked, prior to invocation.If<b>-p</b>is specified, a prompt of the following formatwill be written (in the POSIX locale):<pre><code>"?..."</code></pre>at the end of the line of the output from<b>-t</b>.</blockquote><h4><a name = "tag_001_014_2934"> </a>OUTPUT FILES</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2935"> </a>EXTENDED DESCRIPTION</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2936"> </a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>All invocations of<i>utility</i>returned exit status zero.<dt>1-125<dd>A command line meeting the specified requirements couldnot be assembled, one or more of theinvocations of<i>utility</i>returned a non-zero exit status,or some other error occurred.<dt>126<dd>The utility specified by<i>utility</i>was found but could not be invoked.<dt>127<dd>The utility specified by<i>utility</i>could not be found.</dl></blockquote><h4><a name = "tag_001_014_2937"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>If a command line meeting the specified requirements cannot beassembled, the utility cannot be invoked, an invocation of theutility is terminated by a signal, or an invocation of theutility exits with exit status 255, the<i>xargs</i>utilitywill write a diagnostic message and exit without processing anyremaining input.</blockquote><h4><a name = "tag_001_014_2938"> </a>APPLICATION USAGE</h4><blockquote>The 255 exit status (described as -1 in Issue 3)allows a utility being used by<i>xargs</i>to tell<i>xargs</i>to terminate if it knows no further invocations using thecurrent data stream will succeed.Thus,<i>utility</i>should explicitly<i><a href="chap2.html#tag_001_014_007">exit</a></i>with an appropriate value to avoid accidentally returning with 255.<p>Note that input is parsed as lines; blank characters separate arguments.If<i>xargs</i>is used to bundle output of commands like<i><a href="find.html">find</a></i><i>dir</i><b>-print</b>or<i><a href="ls.html">ls</a></i>into commands to be executed,unexpected results are likely if any filenames contain anyblank charactersornewline characters.This can be fixed by using<i><a href="find.html">find</a></i>to call a script that convertseach file found into a quoted string that 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 were not made consistent herebecause existing applications depend on the current rules andthe shell syntax is not fully compatible with it.An easy rule that can be used to transform any string into a quotedform that<i>xargs</i>will interpret correctly is to precede eachcharacter in the string with a backslash.<p>On implementations with a large value for{ARG_MAX},<i>xargs</i>may produce command lines longer than{LINE_MAX}.For invocation of utilities, this is not a problem.If<i>xargs</i>is being usedto create a text file, users should explicitly set the maximumcommand line length with the<b>-s</b>option.<p>The<i><a href="command.html">command</a></i>,<i><a href="env.html">env</a></i>,<i><a href="nice.html">nice</a></i>,<i><a href="nohup.html">nohup</a></i>,<i><a href="time.html">time</a></i>and<i>xargs</i>utilities have been specified to useexit code 127 if an error occurs so thatapplications can distinguish"failure to find a utility" from "invoked utility exitedwith an error indication."The value 127 was chosen because it is not commonly used for other meanings;most utilities use small values for "normal error conditions" andthe values above 128 can be confused with termination due to receipt of asignal.The value 126was chosen in a similar manner to indicate that the utilitycould be found, but not invoked.Some scripts produce meaningful error messagesdifferentiating the 126 and 127 cases.The distinction between exit codes 126 and 127 is basedon 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 other reason.<br></blockquote><h4><a name = "tag_001_014_2939"> </a>EXAMPLES</h4><blockquote><ol><p><li>The following will move all files from directory$1to directory$2,and echo each move command just before doing it:<pre><code>ls $1 | xargs -I {} -t mv $1/{} $2/{}</code></pre><p><li>The following commandwill combine the output of the parenthesised commands onto one line,which is then written to the end-of-file<b>log</b>:<pre><code>(logname; date; printf "%s\n" "$0 $*") | xargs >>log</code></pre><p><li>The following commandwill invoke<i><a href="diff.html">diff</a></i>with successivepairs of arguments originally typed as command line arguments(assuming there are no embeddedblank charactersin the elementsof the original argument list):<pre><code>printf "%s\n" "$*" | xargs -n 2 -x diff</code></pre><p><li>The user is asked which files in the current directory are to bearchived.The files are archived into<b>arch</b>;a, one at a time, or b, many at a time.<pre><code>a. ls | xargs -p -L 1 ar -r archb. ls | xargs -p -L 1 | xargs ar -r arch</code></pre><p><p><li>The following will executewith successivepairs of arguments originally typed as command line arguments:<pre><code>echo $* | xargs -n 2 diff</code></pre><p></ol></blockquote><h4><a name = "tag_001_014_2940"> </a>FUTURE DIRECTIONS</h4><blockquote>A version supportingthe Utility Syntax Guidelines may be introduced.<p>The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns aboutparts of this interface definition to the IEEE PASC Shell and Utilities Working Groupwhich is identifying the corrections.A future revision of this specification will align withIEEE Std. 1003.2b when finalised.</blockquote><h4><a name = "tag_001_014_2941"> </a>SEE ALSO</h4><blockquote><i><a href="echo.html">echo</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -