为什么 SmartOS (SunOS) 上的 `xargs -n ` 的行为与其他实现不同?

fun*_*oid 1 solaris gnu xargs

-n在 SmartOS(我假设是 Solaris)中找到的 xargs 上的选项似乎与我遇到的任何其他版本的 xargs 不同。

拿这个例子:

内置 /usr/bin/xargs(奇怪的行为):

# printf 'one\0two\0three' | xargs -0 -I{} -n 1 echo "- {}"
- {} one
- {} two
- {} three
Run Code Online (Sandbox Code Playgroud)

GNU Findutils /opt/local/bin/xargs(预期行为):

# printf 'one\0two\0three' | /opt/local/bin/xargs -0 -I{} -n 1 echo "- {}"
- one
- two
- three
Run Code Online (Sandbox Code Playgroud)

来自 MacOS、NetBSD 和 CentOS 的 Xargs 的行为都与上一个示例相同。SmartOS xargs 有什么不同?

来自 SmartOS xargs 联机帮助页:

   -n number
                  Invokes utility using as many standard input arguments
                  as possible, up to number (a positive decimal integer)
                  arguments maximum. Fewer arguments are used if:

                      o      The command line length accumulated exceeds
                             the size specified by the -s option (or
                             {LINE_MAX} if there is no -s option), or

                      o      The last iteration has fewer than number, but
                             not zero, operands remaining.
Run Code Online (Sandbox Code Playgroud)

从 Gnu Findutils xargs 联机帮助页:

   -n max-args, --max-args=max-args
          Use at most max-args arguments per command line.  Fewer than max-args arguments will be used if the size (see the -s option) is  exceeded,  un?
          less the -x option is given, in which case xargs will exit.
Run Code Online (Sandbox Code Playgroud)

我在移植 shell 脚本时发现了这种差异,我很好奇是否有人知道为什么行为不同。

mos*_*svy 6

您不能组合-I-n选项。这就是标准所说的:

-I-L-n选项是相互排斥的。如果在命令行上给出了多个,则某些实现使用指定的最后一个;其他实现以不同的方式处理选项的组合。

另见这个这个

  • @FilipeBrandenburger 我猜 OP 的例子只是为了说明坏掉的错误行为;我已经在链接的答案中解释了如何在不使用“-I”的情况下在 xargs 命令的任何位置插入参数。我不会在这里重复,因为那不是问题的重点。 (3认同)