title: Making bash script to check connectivity and change connection if necessary. Help me improve it?
body: <p>My connection is flaky, however I have a backup one. I made some bash script to check for connectivity and change connection if the present one is dead. Please help me improve them.</p>
  
  <p>The scripts almost works, except for not waiting long enough to receive an IP (it cycles to next step in the until loop too quick). Here goes:</p>
  
  <pre><code>#!/bin/bash
  # Invoke this script with paths to your connection specific scripts, for example
  # ./gotnet.sh ./connection.sh ./connection2.sh
  
  until [ -z "$1" ]  # Try different connections until we are online...
      do
      if eval "ping -c 1 google.com"
      then
          echo "we are online!" &amp;&amp; break
      else
      $1     # Runs (next) connection-script.
      echo
      fi
       shift
     done
  
     echo               # Extra line feed.
  exit 0
  </code></pre>
  
  <p>And here is an example of the slave scripts:</p>
  
  <pre><code>#!/bin/bash
  ifconfig wlan0 down
  ifconfig wlan0 up
  iwconfig wlan0 key 1234567890
  iwconfig wlan0 essid example
  sleep 1
  dhclient -1 -nw wlan0
  sleep 3
  exit 0
  </code></pre>