Bash if / else Loop Error

时间:2016-06-29 13:36:01

标签: bash if-statement syntax-error

正如标题所说。简单的if语句在Bash中。我一直试图弄清楚为什么这个循环不起作用,我现在还没有拿出任何东西两天。洞察力将不胜感激。

declare -r TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S,%3N")

CHECK="$(grep -c "$TIMESTAMP" /var/log/host/logname.log)"
if [ "$CHECK" -eq 0 ]
then
     COLOR="green"
elif [ 0 -lt "$CHECK" ] && [ "$CHECK" -le 5 ]
     COLOR="yellow"
else
     COLOR="red"
fi

COLOR var是由我使用的系统预设的。红色/黄色/绿色都是正确的,我的语法或逻辑都是问题,尽管我一直在努力确定哪个。谢谢!

编辑:我的错误。清晨和问题充斥着错别字。我修好了他们。错误如下,

syntax error near unexpected token `else'
`     else'

2 个答案:

答案 0 :(得分:1)

你忘记了引用,然后是。

declare -r TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S,%3N")   # missing " here

CHECK="$(grep -c "$TIMESTAMP" /var/log/host/logname.log)"
if [ "$CHECK" -eq 0 ]; then
     COLOR="green"
elif [ 0 -lt "$CHECK" ] && [ "$CHECK" -le 5 ]; then    # missing then was here
     COLOR="yellow"
else
     COLOR="red"
fi

答案 1 :(得分:0)

您有一些错误,这可能会有所帮助

declare -r TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S,%3N")
CHECK="$(grep -c "$TIMESTAMP" /var/log/host/logname.log)"
if [ "$CHECK" -eq 0 ]
then
   COLOR="green"
elif [ 0 -lt "$CHECK" ] && [ "$CHECK" -le 5 ]
then
   COLOR="yellow"
else
   COLOR="green"
fi