相关疑难解决方法(0)

为什么循环查找的输出是不好的做法?

这个问题的灵感来自

为什么使用 shell 循环处理文本被认为是不好的做法?

我看到这些结构

for file in `find . -type f -name ...`; do smth with ${file}; done
Run Code Online (Sandbox Code Playgroud)

for dir in $(find . -type d -name ...); do smth with ${dir}; done
Run Code Online (Sandbox Code Playgroud)

几乎每天都在这里使用,即使有些人花时间对这些帖子发表评论,解释为什么应该避免这种东西......
看到此类帖子的数量(以及有时这些评论被简单地忽略的事实)我想我还不如问一个问题:

为什么循环find的输出是不好的做法,为 返回的每个文件名/路径运行一个或多个命令的正确方法是find什么?

find filenames files for

193
推荐指数
4
解决办法
3万
查看次数

如何将 find 找到的文件作为参数传递?

首先切断琐碎但不适用的答案:我既不能使用find+xargs技巧也不能使用它的变体(如findwith -exec),因为我每次调用都需要使用很少的这样的表达式。我会在最后回到这一点。


现在有一个更好的例子,让我们考虑:

$ find -L some/dir -name \*.abc | sort
some/dir/1.abc
some/dir/2.abc
some/dir/a space.abc
Run Code Online (Sandbox Code Playgroud)

我如何将这些作为参数传递给program

只是这样做并不能解决问题

$ ./program $(find -L some/dir -name \*.abc | sort)
Run Code Online (Sandbox Code Playgroud)

失败,因为program得到以下参数:

[0]: ./program
[1]: some/dir/1.abc
[2]: some/dir/2.abc
[3]: some/dir/a
[4]: space.abc
Run Code Online (Sandbox Code Playgroud)

可以看出,带有空间的路径被拆分并program认为是两个不同的参数。

报价直到它起作用

似乎像我这样的新手用户,在遇到此类问题时,往往会随机添加引号,直到它最终起作用为止 - 只是在这里似乎没有帮助......

"$(…)"

$ ./program "$(find -L some/dir -name \*.abc | sort)"
[0]: ./program
[1]: some/dir/1.abc
some/dir/2.abc
some/dir/a space.abc
Run Code Online (Sandbox Code Playgroud)

由于引号可防止分词,因此所有文件都作为单个参数传递。

引用个别路径

一个有前途的方法:

$ ./program $(find -L some/dir …
Run Code Online (Sandbox Code Playgroud)

command-line bash find arguments

9
推荐指数
1
解决办法
8631
查看次数

标签 统计

find ×2

arguments ×1

bash ×1

command-line ×1

filenames ×1

files ×1

for ×1