HyperDevil
HyperDevil

Reputation: 2649

BASH: ping function looping

My ping function seems to loop.
Does anybody know why?

Bash-x output:

++ PING='ping -q -c1'
++ ping -q -c1 -q
++ TEMPIP=-q
++ PING='ping -q -c1'
++ ping -q -c1 -q
++ TEMPIP=-q
++ PING='ping -q -c1'
++ ping -q -c1 -q
++ TEMPIP=-q
++ PING='ping -q -c1'

Code:

IP1="10.1.1.2"
IP2="10.1.1.3" 
IP3="10.1.1.4"
SEC=300

function ping {
   TEMPIP="$1"
   PING="ping -q -c1"

   ${PING} ${TEMPIP}

   if [ $? -ne 0 ]
   then 
       echo 1
   else
       echo 0
   fi

}

  function resetrouter {
  #dosomething
}

function check {
    IP1result=$(ping $IP1)
    IP2result=$(ping $IP2)
    IP3result=$(ping $IP3)
}

check

Upvotes: 0

Views: 867

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85873

If you are going to call your function ping then you should use the full path to the ping command inside the function. You can find this out by calling:

$ which ping
/usr/bin/ping

So PING="ping -q -c1" would become PING="/usr/bin/ping -q -c1"

Upvotes: 0

Robin Green
Robin Green

Reputation: 33093

Because you have called it ping, and the name of the command you want to run is also called ping, meaning it is recursively executing the function, instead of invoking the actual command called ping.

Rename the function ping to some other name.

Upvotes: 4

Related Questions