How to check if the latency of ip is greater than 0?

-2

Code:

 if [ 'ping $1 -c1 |grep rtt |awk {'print$4'} |awk -F "/" {'print$2'}' -gt 0 ]
    then
    echo "OK"
 else
    echo "NO OK"
 fi

Error:

  

./ latency.sh: line 1: [: ping $ 1 -c1 | grep rtt | awk {print} | awk -F   "/" {print}: expected integer expression NO OK

    
asked by anonymous 08.09.2016 / 06:16

2 answers

0

You can compare two float numbers in bash using the bc command:

avg_rtt=$(ping $1 -c1 |grep rtt |awk {'print$4'} |awk -F "/" {'print$2'})

if (( $(echo "$avg_rtt > 0" |bc -l) )); then  
   echo "OK"
else
   echo "NO OK"
fi

Still, you're comparing with 0 (and you can replace it with any float here), which is never expected in a round-trip time (rtt) in ping .

    
09.09.2016 / 16:37
0

To get the values, you can use the command:

ping $1 -c1 | cut -sd '/' -f5

But you still can not make a comparison of a float with integer, in addition to the problems that other users cited.

If you really want to compare the value with zero, you can do it this way:

# Salva o valor de retorno na váriavel $avg
avg=$(ping $1 -c1 | cut -sd '/' -f5)
# Converte a variável $avg para int e compara com zero
if [ ${avg%.*} -gt 0 ]; then
    # retorna OK caso seja maior que zero
    echo "OK"
else
    # retorna NO OK caso seja igual ou menor que zero
    echo "NO OK"
fi
    
10.10.2016 / 20:13