Reputation: 67
I am working on a small script that checks if a host is up or down.
until [ "$STATUS" -eq "0" ]
do
ping -c 1 192.168.0.3
echo The host is down
STATUS=`echo $?`
done
It is supposed to change the status to 0 if it pings a host that is up and exit the until loop. But it doesnt. Even if I echo out the value of $? the value is always zero.
Can anyone help me figure this out please? :)
Thanks in advance
Upvotes: 0
Views: 7724
Reputation: 11489
You have echo The host is down
after ping
command. So $?
takes the exit status of the echo
command not the ping
command.
ping -c 1 192.168.0.3
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "The host is down"
fi
Upvotes: 3
Reputation: 75588
You placed echo after saving the status that's why you always get 0:
ping -c 1 192.168.0.3
echo The host is down ## Always changes $? to 0
STATUS=`echo $?`
One better way to do it could be:
until ping -c 1 192.168.0.3; do
echo "The host is down"
done
Longer version:
until ping -c 1 192.168.0.3; STATUS=$?; [ "$STATUS" -eq "0" ]; do
echo "The host is down"
done
Upvotes: 0