0

I have a small bash script I'm writing to check internet connection on my computer and it contains two boolean variables that change over time. My question is...how do I compare the two?

The code I have is (I thought) pretty straight forward, but it's constantly giving me an "internet OFF" which I know isn't right.

I've tested it in the linux terminal, and I realize that my comparison between booleans isn't correct, I just can't seem to figure it out.

NOTE: I can make the program work if I extend the if/then statement...I just want to know how to make it work the way it's set up below. I don't understand why the boolean variable comparison doesn't work this way.

Thanks for the help!

CODE:

prevStatus=false;
newStatus=true;

while true;
do
   ping -q -c1 www.google.com;
    pingStatus=$?;

    if (($pingStatus == 0));
    then newStatus=true;
    else newStatus=false;
    fi;

    if (($newStatus != $prevStatus)) && (($newStatus == true));
    then echo "internet OFF" >> logfile;
    else echo "internet ON" >> logfile;
    fi;

    prevStatus=$newStatus

    sleep 60s;

done;
AmericanMade
  • 453
  • 1
  • 9
  • 22

1 Answers1

0
prevStatus=false
newStatus=false

while true
do    ping -a www.google.com > /dev/null
pingStatus=$?

prevStatus=$newStatus
if [ $pingStatus == 0 ]
then newStatus=true
else newStatus=false
fi

if [ "$newStatus" != "$prevStatus" ]
 then   
 if [ "$newStatus" == "true" ]
 then echo "internet ON"  >> logfile
 else echo "internet OFF" >> logfile
 fi
fi

prevStatus=$newStatus
sleep 60s
done
Ali ISSA
  • 398
  • 2
  • 10