Zsh:带有空列表的 xargs 的行为 - 这是错误还是功能?
Zsh: Behaviour of zargs with empty list - ist this a bug or a feature?
我今天在 zsh 5.1.1(简化示例)中遇到了以下问题:
#!/bin/zsh
autoload -U zargs
zargs -- my*files(N) -- rm -v
这很好用,直到有一天,没有文件匹配我的模式。因为(N)
,命令变成了
zargs -- -- rm -v
我曾预料到,在这种情况下 rm
根本不会被调用,如果输入为空,xargs
也不会调用命令。但是,rm
被 调用了一次,没有参数,然后产生了写入错误消息的效果。
的确,我们可以看到
zargs -- -- echo xxx
也会调用 echo
一次。
手册页中 zargs
的文档在这方面并不是很清楚,但我认为没有参数,根本不应该调用该命令。
我们有错误吗,或者 zargs
真的应该以这种方式工作吗?
zargs
的工作方式类似于今天的 GNU xargs
(呃...)。
我们可以使用 -r, --no-run-if-empty
选项。
zargs
从 GNU xargs
继承此 属性,从 zargs --help
:
-r, --no-run-if-empty if there are no arguments, then do not run COMMAND;
if this option is not given, COMMAND will be
run at least once
GNU xargs
具有此 功能 :
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run the
command. Normally, the command is run once even if there is no input.
This option is a GNU extension.
--xargs.1
nroff source
(Un?)幸运的是,FreeBSD 和 NetBSD's 具有这样的计数器功能:
-r
Compatibility with GNU xargs. The GNU version of xargs runs the
utility argument at least once, even if xargs input is empty, and
it supports a -r option to inhibit this behavior. The FreeBSD
version of xargs does not run the utility argument on empty
input, but it supports the -r option for command-line compatibility
with GNU xargs, but the -r option does nothing in the FreeBSD
version of xargs.
--FreeBSD xargs(1)
我今天在 zsh 5.1.1(简化示例)中遇到了以下问题:
#!/bin/zsh
autoload -U zargs
zargs -- my*files(N) -- rm -v
这很好用,直到有一天,没有文件匹配我的模式。因为(N)
,命令变成了
zargs -- -- rm -v
我曾预料到,在这种情况下 rm
根本不会被调用,如果输入为空,xargs
也不会调用命令。但是,rm
被 调用了一次,没有参数,然后产生了写入错误消息的效果。
的确,我们可以看到
zargs -- -- echo xxx
也会调用 echo
一次。
手册页中 zargs
的文档在这方面并不是很清楚,但我认为没有参数,根本不应该调用该命令。
我们有错误吗,或者 zargs
真的应该以这种方式工作吗?
zargs
的工作方式类似于今天的 GNU xargs
(呃...)。
我们可以使用 -r, --no-run-if-empty
选项。
zargs
从 GNU xargs
继承此 属性,从 zargs --help
:
-r, --no-run-if-empty if there are no arguments, then do not run COMMAND;
if this option is not given, COMMAND will be
run at least once
GNU xargs
具有此 功能 :
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.
--xargs.1
nroff source
(Un?)幸运的是,FreeBSD 和 NetBSD's 具有这样的计数器功能:
-r
Compatibility with GNU xargs. The GNU version of xargs runs the utility argument at least once, even if xargs input is empty, and it supports a -r option to inhibit this behavior. The FreeBSD version of xargs does not run the utility argument on empty input, but it supports the -r option for command-line compatibility with GNU xargs, but the -r option does nothing in the FreeBSD version of xargs.
--FreeBSD xargs(1)