迭代带有空格的文件列表 [英] Iterate over a list of files with spaces
本文介绍了迭代带有空格的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想遍历文件列表.这个列表是 find
命令的结果,所以我想出了:
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
我可以做些什么来避免空格分割?
What 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屋!
查看全文