user3013172
user3013172

Reputation: 1783

Check file existence every 30 seconds

Why doesn't this work? I need to check the existence of a file every 30 seconds.

STATUS=0
FILENAME="helloworld.file"
while [ $STATUS -eq "0" ] do
if [ -f $FILENAME ];
    then STATUS=1;
else
    sleep 30s;
fi

done

Upvotes: 0

Views: 1337

Answers (2)

Idriss Neumann
Idriss Neumann

Reputation: 3838

You forgot a ; ==>

while [ "$STATUS" -eq "0" ]; do
if [ -f "$FILENAME" ]; then 
    STATUS=1
else
    sleep 30
fi

Or :

while [ "$STATUS" -eq "0" ] 
do
    if [ -f "$FILENAME" ]
    then 
        STATUS=1
    else
        sleep 30
    fi
done

Moreover, do not forget to protect your variables with doubles quotes or favoring the syntax [[.

Here is a reminder about the necessity (or not) to protect your variables with double quotes.

You could also simplify your code :

while true; do
    [[ -f $FILENAME ]] && break
    sleep 30
done

Upvotes: 2

that other guy
that other guy

Reputation: 123460

I don't know, let's ask shellcheck!

In file line 3:
while [ $STATUS -eq "0" ] do
                          ^-- SC1010: Use semicolon or linefeed before 'do' (or quote to make it literal).

Ok, then let's do that:

STATUS=0
FILENAME="helloworld.file"
while [ $STATUS -eq "0" ]
do
if [ -f $FILENAME ];
    then STATUS=1;
else
    sleep 30s;
fi

done

Upvotes: 2

Related Questions