willemdh
willemdh

Reputation: 856

Linux case statement always gives same result

I'm trying to check $VERSION with the case logic, but have some issue.

$VERSION can be "Connection refuced by host", "0,4,1,101 2013-05-18", or some other versions ex "0,4,1,102 2013-09-18"

I always seem to get to "some other problem" while $VERSION is "0,4,1,101 2013-05-18" in my tests...

    #!/bin/bash

HOSTNAME=$1
VERSION=$(/usr/local/nagios/libexec/check_nrpe -H servername01 -c checkversion)
echo "$VERSION" >> /var/log/dig-nscp-install.log

NOW=$(date '+%Y-%m-%d -- %H:%M')
LOGFILE=/var/log/dig-nscp-install.log

#if [ "$TRVER" == "Connection refused by host" ]; then
#       echo  "$NOW : Version $VERSION already installed on HOSTNAME!" >> /var/log/dig-nscp-install.log
#else
#       echo  "$NOW : Powershell install script initiated to install version $VERSION on $HOSTNAME!" >> /var/log/dig-nscp-install.log
#       /usr/local/nagios/libexec/check_nrpe -H servername02 -t 300 -c install_nscp_0.4.101 -a $HOSTNAME
#
#fi

case $VERSION in
        "Connection refused by host")
                echo  "$NOW : Powershell script initiated to install version $VERSION on $HOSTNAME!" >> /var/log/dig-nscp-install.log
                ;;
        "0,4,1,101 2013-05-18")
                echo  "$NOW : Version $VERSION already installed on HOSTNAME!" >> /var/log/dig-nscp-install.log
                ;;
        *)
                echo "$NOW : Some other problem" >> /var/log/dig-nscp-install.log
                ;;

esac

Upvotes: 0

Views: 154

Answers (2)

thom
thom

Reputation: 2332

I ran your code (in bash) and there is nothing wrong with it.
It does what it is supposed to do perfectly :-)

The cause of the problem must be found in the code that assigns a value to "$VERSION".

If you update your question with the missing parts then I try to update my answer accordingly.

UPDATE: tested with VERSION=$( echo "0,4,1,101 2013-05-18" ) and it works perfectly.

I can't run the nagios(?) program that you are running but it is certain that it gives a different output than what you expect (I will serch further and edit this answer accordingly).

Upvotes: 0

Emo Mosley
Emo Mosley

Reputation: 535

My guess is what @chepner mentioned in the comments. You could try something like:

"Connection refused by host"*) <== notice the *

The above should catch any errant carriage return or other weirdness.

If the CR is an issue where VERSION is set, you can alter its assignment by using tr to eliminate the unwanted character as such:

VERSION=$(some command here|tr -d "\r")

Upvotes: 1

Related Questions