【发布时间】:2016-03-27 08:57:50
【问题描述】:
O/S = RHEL 7.2
如您所料,下面的前两行打印“dbg”,然后是 /home/mydir 下的所有文件。但如果我使用 -print0 和 xargs -0,如后两行所示,则第一个文件在目录中被跳过。我尝试将 echo 更改为 echo dbg "$0" $@" 只要子目录中有文件,它就可以工作。在一个空的子目录中,$0 返回“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 "$@"'
【问题讨论】:
-
工作完全符合预期(Linux Mint 17.3,GNU bash,版本 4.3.11(1)-release (x86_64-pc-linux-gnu))。两行给出相同的输出。
-
很高兴知道。我正在 RHEL 7.2 上进行测试。我已经编辑了问题以包括,以及对我所看到的实际行为的一些更正。
-
你不需要
xargs:find "$dir" -maxdepth 1 -type f -exec bash -c '/bin/echo dbg "$@"' _ {} \;。事实上,你甚至根本不需要bash:find "$dir" -maxdepth 1 -type f -exec /bin/echo dbg {} \; -
两者都好。我应该指出这是一个旨在隔离问题的简化示例。在现实生活中,我正在调用用户定义的 bash 函数——这就是我需要调用 bash 的原因。而且我不想使用
find -exec,因为我将调用该函数数十万次。鉴于我的函数中的实用程序的性质(例如 setfacl),使用 xargs 可以为我节省大量时间。