0

我正在尝试使用案例逻辑检查 $VERSION,但有一些问题。

$VERSION 可以是“主机拒绝连接”、“0,4,1,101 2013-05-18”或其他一些版本,例如“0,4,1,102 2013-09-18”

在我的测试中,当 $VERSION 为“0,4,1,101 2013-05-18”时,我似乎总是遇到“其他问题”......

    #!/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
4

2 回答 2

1

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")

于 2013-11-06T16:57:18.783 回答
0

我运行了你的代码(在 bash 中),它没有任何问题。
它完美地完成了它应该做的事情:-)

问题的原因必须在为“$VERSION”赋值的代码中找到。

如果您用缺失的部分更新您的问题,那么我会尝试相应地更新我的答案。

更新:经过测试,VERSION=$( echo "0,4,1,101 2013-05-18" )效果很好。

我无法运行您正在运行的 nagios(?) 程序,但可以肯定它提供的输出与您预期的不同(我将进一步搜索并相应地编辑此答案)。

于 2013-11-06T16:55:00.620 回答