I'm trying to make a script that continuously attempts to ping a domain until it becomes reachable. My aim is to exit with a 0 on success and a 1 on fail after all retry attempts have exceeded.
Here's the script:
#! /bin/bash
attempt_counter=0
max_attempts=20
until $(ping -c 1 "$1"); do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
exit 1
fi
printf '.'
attempt_counter=$(($attempt_counter+1))
sleep 5
done
However, as soon as the ping is a success, the exit doesn't occur and a weird error is displayed instead:
/var/opt/openvpn/wait-on-ping.sh some.domain
ping: unknown host some.domain
.
ping: unknown host some.domain
.
######## AS THIS POINT DOMAIN IS REACHABLE ########
/var/opt/wait-on-ping.sh: line 6: PING: command not found
Is my until condition written incorrectly since my ping exits with 0 on success?