0

I have the following script :

machine="www.google.fr"
coupures=0
while true;
do
  PING=`ping -c 1 $machine`
  if [[ $? -eq 0 ]] ; then
    CMD_DATE=`date`
    DATE=`echo $CMD_DATE | tail -1 | cut -c 17-24`
    TIME=`echo $PING | tail -1 | cut -d/ -f 5`
    if [[ ${TIME} < 20 ]] ; then
        echo ${DATE} : ping sur $machine : `echo -e "\033[30m\033[42m${TIME} ms\033[0m"`
    elif ([ ${TIME} > 20 ] && [ ${TIME} < 99 ]) ; then
        echo ${TIME}
        echo ${DATE} : ping sur $machine : `echo -e "\033[30m\033[43m${TIME} ms\033[0m"`
    elif [[ ${TIME} > 99 ]] ; then
        echo ${TIME}
        echo ${DATE} : ping sur $machine : `echo -e "\033[30m\033[41m${TIME} ms\033[0m"`
    else
        echo Erreur non référencée
    fi
    sleep 1
  else
    #$coupures = $coupures + 1
    $coupures = $coupures + 1
    #var=$((var + 1))

    CMD_DATE=`date`
    DATE=`echo $CMD_DATE | tail -1 | cut -c 17-24`
    echo ${DATE} : Coupure réseau - Impossible de joindre la machine $machine
    echo ${DATE} : Nb de coupures réseau : $coupures
  fi
done
done

But I realize that conditions > 99 do not work.

What should I do?

If TIME is greater than 100, then it executes the first condition instead of the second.

I tried to use -gt/-lt except that the comma causes arithmetic problems.

Sigri44
  • 187
  • 2
  • 13
  • `>` does not mean `greater than` (neither does `<` mean `lower than`) in bash unless you are in an arithmetic context – Aserre Oct 31 '18 at 09:57
  • Also check out http://shellcheck.net/ which can point out many other errors, too (you have numerous). – tripleee Oct 31 '18 at 10:02
  • Thx, but it's an arithmetic context... My option 1 and 2 work fine, but not the 3rd. – Sigri44 Oct 31 '18 at 11:17
  • Besides needing to use -gt and -lt in your tests instead of the arithmetic symbols, you also have a logic flow problem: for all cases where $TIME is greater than 99 it is also greater than 20, so the second test will pass and prevent you from ever getting to the third elif. Switch the order of the -gt 20 and -gt 99 tests. – Beggarman Oct 31 '18 at 11:31
  • I tried to use the -lt/-gt except that the comma is a problem. What I don't understand is why my two conditions work and not the third? And no for your example at 99 I do not agree. There is an exclusive AND so it will not be able to fit into the second condition.... Normally I was in good condition: 1 - if TIME < 20 2 - if TIME < 99 3 - ELSE Except when TIME = 100 it goes in 1 and not in 3! – Sigri44 Oct 31 '18 at 11:38
  • And no for your example at 99 I do not agree. There is an exclusive AND so it will not be able to fit into the second condition.... – Sigri44 Oct 31 '18 at 11:42
  • You're doing a non-arithmetic compare, so 1xx sorted alphabetically is before 9x. To compare numbers you need -gt and -lt, but they only work with integers, so you need to convert. – Beggarman Oct 31 '18 at 12:49
  • Thanks ! It's work fine – Sigri44 Oct 31 '18 at 13:17

0 Answers0