Need some help with checking internet connection script

Hi, I want to use a script which can ping 1.1.1.1 and write "0" or "255" values in /sys/class/leds/tp-link:orange:wan/brightness and /sys/class/leds/tp-link:green:wan/brightness files depending on ping result

Here is a script (I am complete noob in bash):

#!/bin/sh
#

ping -q -w 4 -c 1 1.1.1.1 && if grep -q 0 /sys/class/leds/tp-link:green:wan/brightness> /dev/null ; then /bin/echo "0" > /sys/class/leds/tp-link:orange:wan/brightness;/bin/echo "255" > /sys/class/leds/tp-link:green:wan/brightness;fi && exit 0 || if grep -q 0 /sys/class/leds/tp-link:orange:wan/brightness> /dev/null ; then /bin/echo "0" > /sys/class/leds/tp-link:green:wan/brightness;/bin/echo "255" > /sys/class/leds/tp-link:orange:wan/brightness;fi && exit 0

Question: are && exit 0 in script necessary? I mean, do I need them for succesfull script completion or not? Or without them - script will "stay in ram" or something like that?

there were more than one exit, then yes.

and no, it will not run forever, you'd need a loop for that.

So can I just delete both && exit 0?

So with good ping result script should end before

||

and with bad ping script should not run part && if grep -q 0 /sys/class/leds/tp-link:green:wan/brightness> /dev/null ; then /bin/echo "0" > /sys/class/leds/tp-link:orange:wan/brightness;/bin/echo "255" > /sys/class/leds/tp-link:green:wan/brightness;fi and run only part after ||, right?

I'd do something like this (quick and dirty).

More readable, imho.

It'll loop, if that's something you don't want, remove the 1st and 2 last lines.

#!/bin/sh
#
while true; do
if ping -c 1 1.1.1.1 &> /dev/null
then
  /bin/echo "0"  /sys/class/leds/tp-link:orange:wan/brightness
else
  /bin/echo "255"  /sys/class/leds/tp-link:orange:wan/brightness
fi
sleep 5
done
2 Likes

Ok, thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.