Bash-如何在循环中检查文件是否为空

Sha*_*uan 3 bash shell

我需要检查 Bash 文件是否为空,

如果它是空的,请继续跟踪他,直到写入内容为止。

如果写入了某些内容,则将其回显到屏幕上并停止检查文件内容。

for [ -s diff.txt ]; do
echo "file is empty - keep checking it "
done
echo "file is not empty "
cat  diff.txt 
Run Code Online (Sandbox Code Playgroud)

Tom*_*ech 8

为什么不直接使用while?

while ! [ -s diff.txt ]; do
    echo "file is empty - keep checking it "
    sleep 1 # throttle the check
done
echo "file is not empty "
cat  diff.txt
Run Code Online (Sandbox Code Playgroud)

只要为! [ -s diff.txt ]真,循环就会运行。如果您愿意,可以使用until代替while和删除否定 ( !)。