3

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 

1 Answer 1

10

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 (!).

Sign up to request clarification or add additional context in comments.

1 Comment

Note : '-s' is for testing if a file is not zero size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.