LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   beginner script errors 2 (https://www.linuxquestions.org/questions/linux-newbie-8/beginner-script-errors-2-a-902360/)

greenpool 09-10-2011 08:39 PM

beginner script errors 2
 
Hi guys,

my scripts is not giving me the desired output and i can't seem to figure out why.

all its doing is pinging a host and based on the exit staus print a few lines.

here's the code:
Code:

#!/bin/bash
#Purpose: to ping the gateway 5 times

STATUS=1

COUNT=0

until [[ $STATUS -eq 0 ]]
do
  ping -c 1 192.168.0.45
  RETURNVAL=$? 
  echo "return value: $RETURNVAL"

if [[ $RETURNVAL -eq 0 ]]
  echo "entering loop if return value is 0"
  then
  echo "gateway reachable" 
    let "COUNT+=1"
  echo "count: $COUNT"
  sleep 3
  elif [[ $RETURNVAL -ne 0 ]]
  echo "entering loop if return value is 1" 
  then
    echo "$?"
    echo "gateway unreachable" 

  fi
 
  if [[ $COUNT -eq 5 ]]
  then
  STATUS=0
  fi
done



the issue i'm having is in the following:

Code:

ping -c 1 192.168.0.45
  RETURNVAL=$?

the variable RETURNVAL holds the value 1 (host 192.168.0.45 doesn't exist) but it keeps entering the loop for
Code:

if [[ $RETURNVAL -eq 0 ]]
so my output looks like this:

Code:

PING 192.168.0.45 (192.168.0.45) 56(84) bytes of data.
From 192.168.0.20 icmp_seq=1 Destination Host Unreachable

--- 192.168.0.45 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

return value: 1
entering loop if return value is 0
gateway reachable
count: 1

can somebody please tell me why this is happening..? thanks!

tbrand 09-10-2011 11:20 PM

The ``then'' statement must immediately follow the ``if'' statement.

That's why I usually use the following style:

Code:

if [[ $RETURNVAL -eq 0 ]]; then
  echo "entering loop if return value is 0"
  echo "gateway reachable" 
  let "COUNT+=1"
  echo "count: $COUNT"
  sleep 3
elif [[ $RETURNVAL -ne 0 ]]; then
  echo "entering loop if return value is 1" 
  echo "$?"
  echo "gateway unreachable" 
fi


weibullguy 09-10-2011 11:22 PM

Try moving the lines

echo "entering loop if return value is 0"
echo "entering loop if return value is 1"

AFTER the then statement.

greenpool 09-11-2011 02:27 AM

Thanks a lot guys, that was a fundamental error on my part. cheers!

David the H. 09-11-2011 06:14 AM

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



All times are GMT -5. The time now is 12:32 AM.