xargs - construct argument lists and invoke utility
xargs [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L number] [-l[number]] [-n number [-x]] [-s size] [utility [argument...]]
The xargs utility constructs a command line consisting of the utility and argument operands specified followed by as many arguments read in sequence from standard input as fit in length and number constraints specified by the options. The xargs utility then invokes the constructed command line and waits for its completion. This sequence is repeated until an end-of-file condition is detected on standard input or an invocation of a constructed command line returns an exit status of 255.
Arguments in the standard input must be separated by unquoted blank characters, or unescaped blank characters or newline characters. A string of zero or more non-double-quote (") and non-newline characters can be quoted by enclosing them in double-quotes. A string of zero or more non-apostrophe (') and non-newline characters can be quoted by enclosing them in apostrophes. Any unquoted character can be escaped by preceding it with a backslash (\). The utility are executed one or more times until the end-of-file is reached. The results are unspecified if the utility named by utility attempts to read from its standard input.
The generated command line length is the sum of the size in bytes of the utility name and each argument treated as strings, including a null byte terminator for each of these strings. The xargs utility limits the command line length such that when the command line is invoked, the combined argument and environment lists can not exceed {ARG_MAX}-2048 bytes. Within this constraint, if neither the -n nor the -s option is specified, the default command line length is at least {LINE_MAX}.
The following options are supported:
-e[eofstr]
-E eofstr
-I replstr
-i[replstr]
-L number
-l[number]
-n number
-p
-s size
-t
-x
The following operands are supported:
utility
argument
The 255 exit status allows a utility being used by xargs to tell xargs to terminate if it knows no further invocations using the current data stream succeeds. Thus, utility should explicitly exit with an appropriate value to avoid accidentally returning with 255.
Notice that input is parsed as lines. Blank characters separate arguments. If xargs is used to bundle output of commands like find dir -print or ls into commands to be executed, unexpected results are likely if any filenames contain any blank characters or newline characters. This can be fixed by using find to call a script that converts each file found into a quoted string that is then piped to xargs. Notice that the quoting rules used by xargs are not the same as in the shell. They were not made consistent here because existing applications depend on the current rules and the shell syntax is not fully compatible with it. An easy rule that can be used to transform any string into a quoted form that xargs interprets correctly is to precede each character in the string with a backslash (\).
On implementations with a large value for {ARG_MAX}, xargs can produce command lines longer than {LINE_MAX}. For invocation of utilities, this is not a problem. If xargs is being used to create a text file, users should explicitly set the maximum command line length with the -s option.
The xargs utility returns exit status 127 if an error occurs so that applications can distinguish "failure to find a utility" from "invoked utility exited with 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" and the values above 128 can be confused with termination due to receipt of a signal. The value 126 was chosen in a similar manner to indicate that the utility could be found, but not invoked.
Example 1 Using the xargs command
The following example moves all files from directory $1 to directory $2, and echo each move command just before doing it:
example% ls $1 | xargs -I {} -t mv $1/{} $2/{}
The following command combines the output of the parenthesised commands onto one line, which is then written to the end of file log:
example% (logname; date; printf "%s\n" "$0 $*") | xargs >>log
The following command invokes diff with successive pairs of arguments originally typed as command line arguments (assuming there are no embedded blank characters in the elements of the original argument list):
example% printf "%s\n" "$*" | xargs -n 2 -x diff
The user is asked which files in the current directory are to be archived. The files are archived into arch ; a, one at a time, or b, many at a time:
example% ls | xargs -p -L 1 ar -r arch ls | xargs -p -L 1 | xargs ar -r arch
The following executes with successive pairs of arguments originally typed as command line arguments:
example% echo $* | xargs -n 2 diff
See environ(5) for descriptions of the following environment variables that affect the execution of xargs: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.
PATH
Affirmative responses are processed using the extended regular expression defined for the yesexpr keyword in the LC_MESSAGES category of the user's locale. The locale specified in the LC_COLLATE category defines the behavior of ranges, equivalence classes, and multi-character collating elements used in the expression defined for yesexpr. The locale specified in LC_CTYPE determines the locale for interpretation of sequences of bytes of text data a characters, the behavior of character classes used in the expression defined for the yesexpr. See locale(5).
The following exit values are returned:
0
1-125
126
127
If a command line meeting the specified requirements cannot be assembled, the utility cannot be invoked, an invocation of the utility is terminated by a signal, or an invocation of the utility exits with exit status 255, the xargs utility writes a diagnostic message and exit without processing any remaining input.
See attributes(5) for descriptions of the following attributes:
|
echo(1), shell_builtins(1), attributes(5), environ(5), standards(5)
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |