Ok, I guess I don't really unerstand the 2>&1 part of the script, but your script does not appear to be correct. Here is an example (I removed the >/dev/null so I could see what is going on):
My network is alive and I type:
Code:
(ping -c1 google.com 2>&1) && echo "true"
Here is what that does:
"PING google.com (216.239.37.99) 56(84) bytes of data.
--- google.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms"
notice it doesn't echo "true", which if I'm understanging your script correctly, it should.
So if I do:
Code:
(! ping -c1 google.com 2>&1) && echo "true"
again, while my network is up it says.
"PING google.com (216.239.37.99) 56(84) bytes of data.
--- google.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
true"
According to this, your script
Code:
#!/bin/bash
ROUTER_IP=192.168.9.6
( ! ping -c1 $ROUTER_IP >/dev/null 2>&1 ) && service network restart >/dev/null 2>&1
the network will get restarted even if it's up. Removing the "!" seems to have the desired effect, but it just looks incorrect. What is going on here? Possibly our versions of the "ping" program have different implementations?