Search code examples
bashoutputcomparisonnagios

Capture bash output and compare it to a line of text


I've just joined Stackoverflow because I need some help. I've been doing some basic management of our Red Hat server because we run Nagios on it, but I've been asked to make some scripts for monitoring our SAP environment.

I've modified the SAP servers and a CCMS plugin to retourn the output we need:

EU_PUT History = EU_PUT | 00101401 | Afgebroken |, started at 2016-10-20,00:10:18 terminated at 2016-10-20,00:10:18

I want to use the captured output (^) and check if it contains the word 'Beënd'. If it does it means the job ended succesfully, if it does not it means the job failed (regardless of failed status). Whatever I do, the script does not pick up on the output. The output is succesfully shown when running the below script but the comparison is not working.

What am I doing wrong? Any help would be greatly appreciated!

Kind regards, Dennis Lans

   #!/bin/bash
    T1="*Beënd*"

    #For Nagios reporting purposes:
    unknown="0"
    ok="1"
    warning="2"
    critical="3"

    output=$(/usr/local/nagios/libexec/check_sap job_eu_put lnx '2>&1')

    if [[ "$output" == "$T1" ]]
        then
            echo $output
            exit $ok
        else
            echo $output
            exit $critical

    fi

Solution

  • Enclosing redirections in quotes is not a good practice. More importantly though, your if statement [[ "$output" == "$T1" ]] does a strict comparison, it does not ask if $T1 is a substring of $output. You should use [[ "$output" == *"$T1"* ]] for that. That is, the * should not be placed inside quotes, as you have in your definition of your T1 variable:

    #!/bin/bash
    T1="Beënd"
    
    #For Nagios reporting purposes:
    unknown="0"
    ok="1"
    warning="2"
    critical="3"
    
    output=$(/usr/local/nagios/libexec/check_sap job_eu_put lnx '2>&1')
    
    if [[ "$output" == *"$T1"* ]]
        then
            echo $output
            exit $ok
        else
            echo $output
            exit $critical
    
    fi