Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean type for while loop in bash?

I have a cron script on a shared web host that occasionally gets killed. I'd like to make a loop in bash that tries again if it gets killed, because most of the time it will make it. I'm having trouble with the syntax for storing a boolean value :P

#!/bin/bash
VAR=0;

while [ $VAR ]; do
    if nice -19 mysqldump -uuser -ppassword -h database.hostname.com --skip-opt --all --complete-insert --add-drop-table database_name > ~/file/system/path/filename.sql; then
        VAR=1;
    fi
done

So the script recovers from a killed process okay, but once it's run properly, the new VAR value doesn't kill the while loop.

What am I doing wrong?

like image 279
user151841 Avatar asked Apr 22 '10 19:04

user151841


1 Answers

Try

while [ "$VAR" -eq 0 ]; do

0 and 1 are both considered True because they are not null strings.

like image 52
kennytm Avatar answered Sep 28 '22 06:09

kennytm