我想遍历文件列表。该列表是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