Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm thinking that this needs to be changed to a while loop because, at the moment, it'll wait untill all 10000 pings are done. I need this to return when the ping is successful. What are your suggestions?

#!/bin/bash
echo begin ping
if ping -c 100000 8.8.8.8 | grep timeout;
then echo `say timeout`;
else echo `say the internet is back up`;
fi
share|improve this question
1  
Could you phrase that as a question? –  Ward May 25 '11 at 2:55
add comment

2 Answers

If you want to do it where it keeps checking, and does something different depending on the state, you are right, a while loop is probably a better option. There are a ton of ways to accomplish this, here is a simple example I came up with that would certainly need modification, but could be a start.

#!/bin/bash
PING="/bin/ping -q -c1"
HOST=www.google.com
WAITTIME=3

while [ true ]
do
        ${PING} ${HOST}
        if [ $? -ne 0 ]; then
                echo "Link is down"
        fi
        sleep $WAITTIME 
done

This is an infinite loop, because true always equals true. So this will keep running until you break out of it. Each time through the loop it will ping whatever host is configured by the variable 1 time, then it checks the return value $? of the last command. If that is not 0, then the ping failed, and the assumption is that the link is down. Then it pauses for some period of time (in this case 3 seconds) and tries again because true is still true.

share|improve this answer
add comment

Try this:

#!/bin/bash
while :; do
    ping -c 1 8.8.8.8 >/dev/null 2>&1
    if [ $? = 0 ]; then
        break
    else
        echo timeout
    fi
    sleep 1
done
echo the internet is back up

If you want to use "say" you just need to change the lines with "echo".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.