Linux 脚本,用于目录的文件

Linux Script, for on files of a directory

我需要做一个 shell 脚本来在目录中的文件上方发送一些参数。

我正在使用

for f in *.tmp
do
        echo  $f
        sleep 5
done

我的问题是在执行for的过程中,目录中的文件数可能会发生变化。而for只适用于第一次执行时列出的文件。

有什么想法吗?? 非常感谢!

您可以将文件存储在数组中

files=(*.tmp)

然后使用无限while循环,满足条件时跳出:

i=0
while true; do
    do_something with "${files[i]}"
    current_files=(*.tmp)
    magic_handwaving -- some code to compare "files" with "current_files" \
        that will append new files to "files"
    (( ++i == ${#files[@]} )) && break
done
echo all files processed