击:遍历用空格的文件列表 [英] Bash : iterate over list of files with spaces
本文介绍了击:遍历用空格的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要遍历文件列表。这份名单是一个找到
命令的结果,所以我想出了:
I want to iterate over a list of files. This list is the result of a find
command, so I came up with:
getlist() {
for f in $(find . -iname "foo*")
do
echo "File found: $f"
# do something useful
done
}
这很好,除非文件在其名称中的空格:
It's fine except if a file has spaces in its name:
$ ls
foo_bar_baz.txt
foo bar baz.txt
$ getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt
我该怎么做才能避免空间分割?
How can I do to avoid the split on spaces ?
推荐答案
您可以用替换基于字的迭代基于行之一:
You could replace the word-based iteration with a line-based one:
find . -iname "foo*" | while read f
do
# ... loop body
done
这篇关于击:遍历用空格的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文