Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6159

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

I need to check in Bash file is empty,

if it's empty keep tracking him until something is written to it.

In case something was written to it echo it to the screen and stop checking the file content.

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

Upvotes: 3

Views: 11256

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74695

Why not just use a 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

The loop will run as long as ! [ -s diff.txt ] is true. If you prefer, you can use until instead of while and remove the negation (!).

Upvotes: 10

Related Questions