繁体   English   中英

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

[英]Bash- How to check if file is empty in a loop

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

如果它是空的,继续跟踪他,直到有东西被写入。

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

for [ -s diff.txt ]; do
echo "file is empty - keep checking it "
done
echo "file is not empty "
cat  diff.txt 

为什么不用一段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

循环将运行只要! [ -s diff.txt ] ! [ -s diff.txt ]是真的。 如果你愿意,你可以使用until而不是while删除否定( ! )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM