sleep <number of seconds>
I am assuming here that you are trying to script usbmodeswitch to startup your cellular modem.
I guess, the answer depends on several things:
E.g. consider a host you want to access from your script using curl(), but its web server died, so you'll get timeout, but you can still ping it.
- presence of any host or a particular host?
- access it with ping, ssh, http etc?
!/bin/sh
server=192.168.1.1
ping_response=-1
while [ "0" != "\$ping_response" ]; do
echo Waiting to let network connections settle ...
sleep 1
ping -qc 1 $server > /dev/null
ping_response=\$?
done
!/bin/sh
server=192.168.1.1
to_limit=30 #--- 30 retry timeout limit
to_count=0
ping_response=-1
while [ "0" != "$ping_response" ]&&[ $to_count -lt $to_limit ]; do
echo Waiting to let network connections settle ...
sleep 1 #--- 1 second timeout
ping -qc 1 -W 0 $server > /dev/null 2>&1
ping_response=$?
if [ "0" != "$ping_response" ]; then
to_count=$(($to_count+1))
fi
done
if [ "0" = "$ping_response" ]; then
echo "Response received from "$server" after "$to_count" retries."
else
echo "No response from "$server" after "$to_count" retries."
fi
-W 0
in the ping
syntax is necessary to avoid unwanted additional wait times. 2>$1
is needed to avoid error output from ping, such as "ping: sendto: Host is down"-t timeout
Specify a timeout, in seconds, before ping exits regardless of
how many packets have been received.
First of all, you must not escape the dollar sign in your script. Otherwise it will assign the actual string "$?" to your variable.
-ot 30
it tries to ping the host for 30 seconds. As soon as one reply is received, it exits with exit code 0 (true). If it didn't receivce a reply within 30 seconds, it exits with exit code 2 (false). So you can simply use this snipped in your script:...
if ping -ot 30 $server >/dev/null 2>&1 ; then
echo "Server is alive."
else
echo "No response from server."
fi
...