我想遍历文件列表。这个列表是一个find命令的结果,所以我想出了:
find
getlist() { for f in $(find . -iname "foo*") do echo "File found: $f" # do something useful done }
没关系,除非文件的名称中有空格:
$ 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
我能做些什么来避免空间分割?
您可以将基于单词的迭代替换为基于行的迭代:
find . -iname "foo*" | while read f do # ... loop body done