N
N
Nastya19202021-09-17 13:19:39
zsh
Nastya1920, 2021-09-17 13:19:39

How to create a while-do loop in a script to check if the file is empty?

This is my first time writing a script.
You need to create something like this: check the file until it turns out that it exists and is not empty (there will be a certificate in the file)

While [test shows that the file either does not exist or is empty]
Do
Check again if the file exists and is empty file
Done

Tried this, doesn't work:

IsFileEmptyOrMissing(){
file="$1"
[ ! -s "${file}" ] && return 0 || return 1
}

IsFileEmptyOrMissing "ca.crt" >res
Cat res (empty)
x=$(cat res)

while [ $x != 0 ]
Do
IsFileEmptyOrMissing "ca.crt" >res
x=$(cat res)
Done

The file "ca.crt" now definitely exists for the test, it lies in the folder along with the script.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-09-17
@Nastya1920

check the file until it appears that it exists and is not empty

You can do something like this:
while [ ! -s "$file" ] ; do sleep 1 ; done
While [test shows file either does not exist or is empty]
Do
Check again if file exists and if file is empty
Done

this is some kind of painfully loaded cycle, you don’t have to do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question