双字符串比较失败,正确的字符串报告

时间:2016-05-02 11:15:58

标签: bash if-statement

如此简单,所以不能同时工作..

#!/usr/bin/env bash

RES=$(tail -1 $1)
VAR2=$(grep -oh $2 $1 | tail -1)
VAR3=$(grep -oh $3 $1 | tail -1)

#echo "res=${RES} | var2=${VAR2} - var3=${VAR3}"
#exit 0

if [ ! -f $1 ]; then
  echo "Log file not found"
  exit 3
else
  if [[ ${RES} == ${VAR2}  &&  ${RES} != ${VAR3} ]]; then
    echo $RES
    exit 0
  fi
  if [[ ${RES} == ${VAR2}  &&  ${RES} == ${VAR3} ]]; then
    echo $RES
    exit 1
  fi
  if [[ ${RES} != ${VAR2}  &&  ${RES} == ${VAR3} ]]; then
    echo $RES
    exit 2
  fi
  echo $RES
  exit 3
fi

这是一个帮助我阅读日志文件的nagios脚本。 我用/usr/local/.../read_log.sh OK NOK

称这个脚本(来自nagios)

如果我取消注释

#echo "res=${RES} | var2=${VAR2} - var3=${VAR3}"
#exit 0

我从Nagios看到

Status Information: res=Backup of Monday: OK: Backup of CURRENT: NOK
Performance Data:   var2=OK - var3=NOK

哪一个应该是退出1的第二个IF。但它不会是什么,如果log包含OK和/或NOK,它总是用于Unable to read output

修改 使用此代码:

  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo "$RES exit 0"
    exit 0
  fi
  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo "$RES exit 1"
    exit 1
  fi
  if [[ ! ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo "$RES exit 2"
    exit 2
  fi

输出是:

user@server:/usr/local/nagios/libexec# ./read_last_line.sh /var/log/Samba_backup.log OK error
Backup of Monday: OK - Backup of CURRENT: OK exit 1

编辑3: 好吧我找到了一些有趣的东西。当$ RES仅包含OK时,$ VAR3为"",为空。

在主要之前如果我添加:

if [ -z $VAR3 ]; then
  VAR3=error
fi

脚本exit 1找到"错误"和OK exit 0。 我想我会清理它并写得更好。如果有人可以使用$ VAR =""的条件发布完整代码我将这个问题解决了!

编辑4: 并且,为了匹配每个案例,这是从EDIT 3到$ VAR2的完整代码:

if [ ! -f $1 ]; then
  echo "Log file not found"
  exit 3
else
  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 0
  fi
  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 1
  fi
  if [[ ! ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 2
  fi
  if [[ ! ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 2
  fi
  echo $RES
  exit 3
fi

编辑5:这应该是最后一个版本,修改自原始和最新代码(并感谢)@jgshawkey

RES=$(tail -1 $1)
VAR2=$(grep -oh $2 $1 | tail -1)
VAR3=$(grep -oh $3 $1 | tail -1)

[[ -z ${VAR2} ]] && VAR2=$2
[[ -z ${VAR3} ]] && VAR3=$3


# if $VAR2 exists
if [[ $(echo ${RES} | egrep "$VAR2") ]]; then
  # and if $VAR3 does not exist
  if [[ ! $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 0
  # and if $VAR3 exists too
  elif [[ $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 1
  fi
# else if $VAR2 does not exist
else
  # and if $VAR3 does not exists
  if [[ ! $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 1
  # and if $VAR3 exists too
  elif [[ $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 2
  fi
fi

2 个答案:

答案 0 :(得分:2)

您希望匹配单词" OK"和" NOK"反对更长的字符串。从正确的角度来看:

"周一备份:好的:当前备份:NOK"不等于"好的"或" NOK"。您想要搜索字符串中的模式。请参阅下面的修订代码。这将寻找" :好的"或" :NOK "在字符串$ RES。

我希望这会有所帮助。

if [ ! -f $1 ]; then
  echo "Log file not found"
  exit 3
else
  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 0
  fi
  if [[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 1
  fi
 if [[ ! ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]]; then
    echo $RES
    exit 2
  fi
  echo $RES
  exit 3
fi

- 编辑

VAR2和VAR3是从$ RES或其他来源拉? 什么都可以设置VAR2和VAR3?到目前为止,我已经看到:VAR2和VAR3等于"",OK或NOK。 所有可以设置为什么? 一旦我知道我们正在处理什么,我们可以解决剩下的问题。

# assumptions
# RES is something like "Backup of Monday: OK: Backup of CURRENT: NOK"
# VAR2 is "OK" or ""
# VAR3 is "OK" or ""

# if $VAR2|$VAR3 are blank set them to "OK"
[[ -z ${VAR2} ]] && VAR2="OK"
[[ -z ${VAR3} ]] && VAR2="OK"

# if file does not exist exit 3
[[ ! -f $1 ]] && echo "Log file not found" && exit 3

[[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]] && echo ${RES} && exit 0
[[ ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ${RES} =~ ^.*:.${VAR3}.*$ ]] && echo ${RES} && exit 1
[[ ! ${RES} =~ ^.*:.${VAR2}.*$ ]] && [[ ! ${RES} =~ ^.*:.${VAR3}.*$ ]] && echo ${RES} && exit 2

- 编辑

让这更容易。

您可以在VAR2和VAR3的声明中使用多个搜索项。 例如:VAR2 =" ^ $ | OK"。这将匹配一个空行或单词"错误"。
这应该更容易编写脚本。

RES=$(tail -1 $1)
VAR2=$(grep -oh $2 $1 | tail -1)
VAR3=$(grep -oh $3 $1 | tail -1)

[[ -z ${VAR2} ]] && VAR2="OK"
[[ -z ${VAR3} ]] && VAR3="OK"


# if $VAR2 okay
if [[ $(echo ${RES} | egrep "$VAR2") ]]; then
  # and if $VAR3 not okay
  if [[ ! $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 0
  # and if $VAR3 okay
  elif [[ $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 1
  fi
# else if $VAR2 not okay
elif [[ ! $(echo ${RES} | egrep "$VAR2") ]]; then
  # and if $VAR3 not okay
  if [[ ! $(echo ${RES} | egrep "$VAR3") ]]; then
    echo ${RES}
    exit 2
  fi 

答案 1 :(得分:1)

我认为比较错误是因为您的RES变量等于Backup of Monday: OK: Backup of CURRENT: NOKVAR2等于OK所以您如何搜索它?

我认为你应该使用:

if [ "${RES}" != *"NOK"* ] ; then

确保NOK不可用。

@chepner在评论中指出: 您可以使用上面的符号或使用双方括号并省略引号(")。

请参阅HEREHERE以供参考。