http://pastebin.com/raw.php?i=rykHdvBh
bix.hu and www.yahoo.com are "pingable" test sites. 127.0.0.1 could not be pinged [firewall drops all icmp]
i have a "oneliner" that echoes if theres "internet connection or no". $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" internet connection ok $ ping -W 1 -c 2 127.0.0.1 >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 127.0.0.1 >& /dev/null && ping -W 1 -c 2 127.0.0.1 >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 127.0.0.1 >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" internet connection ok $
Ok!
But: if i want the "oneliner" to only go along when theres internet connection: $ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 127.0.0.1 >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 127.0.0.1 >& /dev/null && ping -W 1 -c 1 127.0.0.1 >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 127.0.0.1 >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $
It just doesn't work.
Goal: if theres no internet connection, then the oneliner must loop until there is internet connection. if theres internet connection the oneliner ends.
what am i missing?
On 01/28/2011 11:28 AM, kellyremo wrote:
http://pastebin.com/raw.php?i=rykHdvBh
bix.hu and www.yahoo.com are "pingable" test sites. 127.0.0.1 could not be pinged [firewall drops all icmp]
i have a "oneliner" that echoes if theres "internet connection or no". $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" internet connection ok $ ping -W 1 -c 2 127.0.0.1 >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 127.0.0.1 >& /dev/null && ping -W 1 -c 2 127.0.0.1 >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 127.0.0.1 >& /dev/null && echo "internet connection ok" || echo "no internet connection" no internet connection $ ping -W 1 -c 2 bix.hu >& /dev/null && ping -W 1 -c 2 www.yahoo.com >& /dev/null && echo "internet connection ok" || echo "no internet connection" internet connection ok $
Ok!
But: if i want the "oneliner" to only go along when theres internet connection: $ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 127.0.0.1 >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 127.0.0.1 >& /dev/null && ping -W 1 -c 1 127.0.0.1 >& /dev/null && TORF=false || TORF=true; done $ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 127.0.0.1
& /dev/null && TORF=false || TORF=true; done
$ while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; done $
It just doesn't work.
Goal: if theres no internet connection, then the oneliner must loop until there is internet connection. if theres internet connection the oneliner ends.
what am i missing?
Instead of using a variable (that has funky scoping in shell) just make the && compound command the loop condition and place it in a subshell:
while (! ping -W 1 -c 1 bix.hu && ! ping -W 1 -c 1 www.yahoo.com )&>/dev/null; do /bin/true ; done
Or you can write the same as:
while (! (ping -W 1 -c 1 bix.hu && ping -W 1 -c 1 www.yahoo.com))&>/dev/null; do /bin/true ; done
It's probably not the best way to do it though - this little shell loop eats up fair bit of CPU time.
Just putting a "sleep 1" instead of /bin/true makes the load barely noticable but if you're on a system that's using Network Manager then you could also look at the information provided by the nmcli tool to see if it would meet your requirements.
Regards, Bryn.
kellyremo <kellyremo <at> zoho.com> writes:
... Goal: if theres no internet connection, then the oneliner must loop until there is internet connection. if theres internet connection the oneliner ends.what am i missing? ...
Test:
1. there is internet connection to both
$ TORF=true; while $TORF; do ping -W 1 -c 1 bix.hu >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; echo $TORF; sleep 1; done false $
2. there is no internet connection to at least one
$ TORF=true; while $TORF; do ping -W 1 -c 1 www.blakstar.org >& /dev/null && ping -W 1 -c 1 www.yahoo.com >& /dev/null && TORF=false || TORF=true; echo $TORF; sleep 1; done true true true true ^C $
Nota bene ! Let's assume negation is represented by "~" sign. ~(a && b) equivalent to ~a || ~b ~(a || b) equivalent to ~a && ~b
JB