xargs 的示例-x 选项的作用
GNU xargs
有选项“-x”。手册页说:
-x 如果超出大小(请参阅 -s 选项)则退出。
但是 xargs 似乎并不关心是否设置了 -x 。我无法举出一个 -x
具有任何效果的示例。
请提供两个示例,其中唯一的区别是添加了 -x
并产生不同的输出。
GNU xargs
has option '-x'. The man page says:
-x Exit if the size (see the -s option) is exceeded.
But xargs
seems to not care if -x
is set or not. I have been unable to make an example in which the -x
has any effect at all.
Please provide two examples in which the only difference is an added -x
and that produce different output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你必须设置一个大小来测试你是否超过了它。
You have to set a size to test whether you've exceeded it.
这当然取决于您使用的
xargs
类型,但流行的 GNU findutils 的xargs
(很可能就是您所拥有的)具有这种行为,违反了最小惊讶原则:它通过重复读取下一个输入项并将其附加到现有命令行,直到它不适合行长度,在内存中累积命令行......现在,它检查读取的项是否会“ t 甚至可以作为单个参数放入一行(示例中的echo 1234111
就是这种情况):(上面的代码接近函数
read_line()
的末尾在 findutils-4.6.0/xargs/xargs.c 中,与 findutils-4.4.2 中几乎相同)这是选项
-x
(对应于bc_ctl.exit_if_size_exceeded
)的位置区别 - 使用-x
,exec_if_possible()
只是返回
,而没有bc_do_exec()
已经积累的好-形成的命令(在您的示例中为echo 10 11
)。然后,在return
之后,函数error()
退出xargs
程序。因为如果输入项本身太长,则所述 xargs 总是 退出,因此所有后续项都会丢失。
This depends of course on the kind of
xargs
you're using, but the prevalent GNU findutils'xargs
(which may well be the one you have) has exactly this behavior, violating the principle of least astonishment: It accumulates a command line in memory by repeatedly reading the next input item and appending it to the existing command line until it doesn't fit in the line length… Now, it checks whether the read item wouldn't even fit in a line as a single argument (which is the case withecho 1234111
in your example):(the above code is near the end of function
read_line()
in findutils-4.6.0/xargs/xargs.c, nearly same in findutils-4.4.2)Here's where option
-x
(corresponding tobc_ctl.exit_if_size_exceeded
) makes a difference - with-x
,exec_if_possible()
justreturn
s withoutbc_do_exec()
ing the already accumulated well-formed command (echo 10 11
in your example). Then afterreturn
ing, the functionerror()
exits thexargs
program.Because said
xargs
always exits if an input item is too long by itself, all following items are lost.tl;dr
使用
-x
仅与-n
一起使用才有意义:< strong>
参数(如果可用)必须适合单个命令行,基于隐式字节计数最大值或显式指定-s
选项;如果不是,xargs
将退出并出现错误。如果没有
-x
,将构造一个更少参数的命令行,并且不会发生错误 - 除非甚至没有单个< /em> 参数适合 - 请参阅下一点。-x
逻辑隐式适用于-L
和-I< /code> 选项。
警告:从 OSX 10.11.3 和 PC-BSD 10.1 开始,
的 BSD 实现中出现了问题 - 请参阅下文。-x
和-n
的组合似乎是 < xargs如果没有
-n
,就没有理由使用-x
,因为两个xargs
实现总是当遇到单个参数时退出并返回错误,该参数将导致命令对于整体字节大小限制太长(同样,无论是隐含的还是用指定的) -s
)。xargs
:-x
没有-n
表面bug(从 v4.2.2 开始),如Armali 的有益回答中所述。xargs
:不带-n
的-x
语法上不支持 .-x
,单个太长的参数也会导致错误:1 # ok: 'echo 1' 是 6 个字节
xargs: 参数行太长 # 'echo 10' 太长
以下 GNU
xargs
示例:-x
)或必须使用( 与-x
) 对于每个命令实例:-n 2
-s 9
注意:对于 BSD
xargs
,no-x
示例下面的工作原理相同,-x
示例应该工作,但不适用于 OSX 10.11.3 和 PC 上的xargs
版本-BSD 10.1:每当指定-x
时,-n
中指定的
值将被忽略,并且xargs
的行为就像已指定-n 1
一样。请注意,在最后一次成功执行的命令之前,如何奇怪地打印错误消息;这可能与上面提到的错误有关,因此 Armali 的回答 可能会提供解释。
tl;dr
Use of
-x
only makes sense with-n <argumentCount>
:<argumentCount>
arguments, if available, must fit on a single command line, based on either the implicit byte-count maximum or an explicitly specified-s <byteCount+1>
option; if not,xargs
exits with an error.Without
-x
, a command line with fewer arguments would be constructed, and no error would occur - unless not even a single argument fits - see next point.-x
logic implicitly applies to the-L <lineCount>
and-I <placeHolder>
options.-x
and-n
appears to be broken in the BSD implementation ofxargs
- see below.Without
-n
, there is NO reason to use-x
, because bothxargs
implementations always exit with an error when they encounter a single argument that would result in a too-long command for the overall byte-size limit (again, either implied or specified with-s <byteCount+1>
).xargs
:-x
without-n
surfaces a bug (as of v4.2.2), as unearthed in Armali's hepful answer.xargs
:-x
without-n
is syntactically not supported.-x
, a single too-long argument causes an error:$ echo 1 10 | xargs -s 7 echo # max. command length is 6 (7 - 1) bytes
1 # ok: 'echo 1' is 6 bytes
xargs: argument line too long # 'echo 10' is too long
The following GNU
xargs
examples:-x
) or must be used (with-x
) for each command instance:-n 2
-s 9
Note: With BSD
xargs
, the no-x
example below works the same, and the-x
example should work, but doesn't with thexargs
version found on OSX 10.11.3 and PC-BSD 10.1: whenever-x
is specified, the specified<argumentCount>
value in-n <argumentCount>
is ignored andxargs
behaves as if-n 1
had been specified.Note how the error message is curiously printed before the last successfully executed command; this is possibly connected to the bug mentioned above, so Armali's answer may offer an explanation.