The bracket tests are really designed for use with string values. In bash, when testing numerical values such as exit codes, the
arithmetic operator is recommended.
Code:
if (( RETURNVAL == 0 )); then
Or since 0 evaluates as false in numeric tests, and !0 evaluates as true (some value exists), you can even just do this:
Code:
if (( RETURNVAL )); then
This does reverse the logic of the
if construct, however.
There's also no need to do use
elif here, since the result is just the inverse of the first test. "
else" only will do. elif is only needed when you have two or more
independent tests.
Code:
if (( RETURNVAL )); then
echo "entering loop if return value is 1"
echo "$?" #see the note below about this line
echo "gateway unreachable"
else
echo "entering loop if return value is 0"
echo "gateway reachable"
(( COUNT+=1 ))
echo "count: $COUNT"
sleep 3
fi
This doesn't do what you think it does, however.
Code:
echo "entering loop if return value is 1"
echo "$?"
$? will always be "0" here, because it contains the exit code of the
previous command, which is
echo.
Edit: Come to think of it, a case construct would also be a viable option here.
Code:
case "$RETURNVAL" in
0) echo "entering loop if return value is 0"
echo "gateway reachable"
(( COUNT+=1 ))
echo "count: $COUNT"
sleep 3
;;
*) echo "entering loop if return value is 1"
echo "gateway unreachable"
;;
esac