不工作:查找-print0 | xargs -0 bash -c "...."

Not working: find -print0 | xargs -0 bash -c "...."

O/S = RHEL 7.2

如您所料,下面的前两行打印“dbg”,紧接着是 /home/mydir 下的所有文件。但是如果我使用 -print0 和 xargs -0,如后两行所示,目录中的第一个文件被跳过。我尝试将 echo 更改为 回声 dbg "$0" $@" 只要子目录中有文件,这就有效。在空子目录上,$0 returns "bash"。

# This works
dir=/home/mydir
find "$dir" -maxdepth 1 -type f -print | xargs  bash -c '/bin/echo dbg "$@"'  

# This skips the first file
dir=/home/mydir
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 bash -c '/bin/echo dbg "$@"'

当您使用 bash -c 'command' arg0 arg1 arg2 … 时,表示为 arg0 的参数被视为 [=14=],脚本的名称(因此不是 "$@" 的一部分) .

您可以通过以下方式进行演示:

$ bash -c 'echo dbg "$@"' a b c
dbg b c
$ bash -c 'echo dbg "$@"' name a b c
dbg a b c
$ bash -c 'echo dbg [[=10=]] "$@"' name a b c
dbg [name] a b c
$

使用:

find "$dir" -maxdepth 1 -type f -print0 | xargs -0 bash -c '/bin/echo dbg "$@"' name