A rough cut at it would be to use the bash special variable $SECONDS, which counts the number of seconds since the shell started. I've made three changes to the script:
- changed the sh-bang line from /bin/sh to /bin/bash
- added a second condition to the
while test to compare $SECONDS to 50
The new script:
while ! ping -c1 "$1" &>/dev/null; [[ "$SECONDS" -lt 50 ]]
do echo "Ping Fail - `date`"
echo "Host Found - `date`"
I would just note that the
Host Found statement is potentially misleading in the case of a 50-second timeout. You could compare $SECONDS to 50 after the loop to determine whether the timeout occurred.
This is a rough estimate of 50 seconds, since the loop could be entered with $SECONDS == 49, and then
ping could take more than one second to succeed or fail.