Search code examples
bashvariablesnumerical-methods

Perform "bigger than" on more than two digits


I've tried to google this but I'm not sure how I should form my search correctly. I'm new at programming and I've managed with the help of this community and google, create an OP5 check that's written in bash.

The OP5 checks calls an expect script that logs on to a device using SSH and performs a set of commands and writes the output to a file. The file will contains the following text:

"Count: 46"

In the OP5 check I grep out the numerical value and then form a "bigger than" statement that compares the variable from the expect script and the -c (critical) -w (warning limit) variables that's set when the script runs.

Now here's my dilemma. It seems that the "bigger than" statement only reads the two digits. When I set a warning limit at 1500 and a critical limit at 1700. It reads it as "15" and "17" thus causing a critical warning in the surveillance program.

Currently the variable that I create using grep looks like this:

VPNCount=$(grep -o '[0-9]\+' $ExpectFolder/$HOSTADDRESS.op5.vpn.results)

Is there something I can do to make the script understand that 1500 and 1700 is a bigger value than 46 and so that it reads at least 5 digits?


Solution

  • Without the comparison statement used, it is not easy to figure what could have gone wrong but here is a stab in the dark:

    Notice that if the comparison is string-based, "46" is larger than "1500"

    if [[ 46 > 1500 ]]; then echo true; fi
    > true
    

    On the other hand if you do a integer comparison, you will get the correct result:

    if [ 46 -gt 1500 ]; then echo true; fi
    >
    

    Could this have been the issue?

    More info here.