Bash脚本用于计算文件中字符串的出现次数

时间:2013-09-16 15:53:53

标签: bash if-statement

我在Bash脚本中有这个功能

 if [ $numberOne -gt 10 ]
        then
                echo "$numberOne has occurred over 10 times"
                echo "email me numberOne"

        elif [ $numberTwo -gt 4 ]
        then    echo "$numberTwo has occurred over 4 times"
                echo "email me numberTwo"

        elif [ $numberThree -gt 4 ]
        then    echo "$numberThree has occurred over 4 times"
                echo "email me numberThree"

        elif [ $numberFour -gt 5 ]
        then    echo "$numberFour has occurred over 5 times"
                echo "email me numberFour"

        else    echo "nothing found yet"
                exit


        fi

}

信息:我正在进行端口检查。我每分钟运行一个脚本。当发现使用端口时,我将其写入文件然后读取文件。它被发现的次数等于分钟。 4次是4分钟,我想知道端口是否活动多了几分钟。

我想做什么:

3个数字将填充文件。在某些时候,其中一个将出现超过4次,其余的也将出现。此时我想要在第一次发生时发出警报,或者在第一次发生之后发出所有警报。理想情况下,警报将是这样的:“NumberOne已打开4分钟”。 1分钟后它将是“5分钟” - 直到我停止它或达到阈值,我还不知道。

问题: 这里的问题是,当NumberOne发生超过10次且NumberTwo发生4次之后,它只回显NumberTwo。

我想我可以在continue之后使用then,但我不能!

另外:我的NumberOne变量。 NumberOne=$(grep -wc "port=51555" monitor.txt)

3 个答案:

答案 0 :(得分:3)

要获取字符串在文件中出现的次数,请使用grep -c

grep -c something file

一个示例应用程序是:

file="/path/to/file"
numberOne_string="something"
numberOne=$(grep -c "$numberOne_string" "$file")

关于你的逻辑,我认为最好的选择只能是:

if [[ numberOne -gt 10 || numberTwo -gt 4 || numberThree -gt 4 || numberFour -gt 5 ]]; then
    if [[ numberOne -gt 10 ]]; then
        echo "$numberOne has occurred over 10 times"
        echo "email me numberOne"
    fi
    if [[ numberTwo -gt 4 ]]; then
        echo "$numberTwo has occurred over 4 times"
        echo "email me numberTwo"
    fi
    if [[ numberThree -gt 4 ]]; then
        echo "$numberThree has occurred over 4 times"
        echo "email me numberThree"
    fi
    if [[ numberFour -gt 5 ]]; then
        echo "$numberFour has occurred over 5 times"
        echo "email me numberFour"
    fi
else
    echo "nothing found yet"
fi

或者否定版本。

另一个需要一个变量:

nothing_found=true
if [[ numberOne -gt 10 ]]; then
    echo "$numberOne has occurred over 10 times"
    echo "email me numberOne"
    nothing_found=false
fi
if [[ numberTwo -gt 4 ]]; then
    echo "$numberTwo has occurred over 4 times"
    echo "email me numberTwo"
    nothing_found=false
fi
if [[ numberThree -gt 4 ]]; then
    echo "$numberThree has occurred over 4 times"
    echo "email me numberThree"
    nothing_found=false
fi
if [[ numberFour -gt 5 ]]; then
    echo "$numberFour has occurred over 5 times"
    echo "email me numberFour"
    nothing_found=false
fi
if [[ $nothing_found == true ]]; then
    echo "nothing found yet"
fi

答案 1 :(得分:0)

您正在检查不同的变量...您不需要elif ...使用独立if更改它。例如:

if [ $numberOne -gt 10 ]
then
    echo "$numberOne has occurred over 10 times"
    echo "email me numberOne"
fi

if [ $numberTwo -gt 4 ]
then
    echo "$numberTwo has occurred over 4 times"
    echo "email me numberTwo"
if

if [ $numberThree -gt 4 ]
then
    echo "$numberThree has occurred over 4 times"
    echo "email me numberThree"
fi

if [ $numberThree -gt 5 ]
then
    echo "$numberFour has occurred over 5 times"
    echo "email me numberFour"
fi

if [ $numberOne -le 10 -a $numberTwo -le 4 -a $numberThree -le 4 -a $numberFour -le 5 ]
then
    echo "nothing found yet"
    exit
fi

答案 2 :(得分:0)

这是一个减少代码重复的重构。

found=false
while read variable count; do
    value=${!$variable}
    if [ "$value" -gt "$count" ]; then
        echo "$value occurred more than $count times"
        echo "email me $variable"
        found=true
    fi
done <<____HERE
    numberOne  10
    numberTwo   4
    numberThree 4
    numberFour  5
____HERE

if ! $found; then
    echo "Nothing found yet"
fi

如果您没有Bash,则可以改为使用eval value="\$$variable"

如果要动态填充变量,可以在循环内执行grep -c,然后放置,例如改为使用正则表达式,或者只是端口号,而不是这里的文档的第一列。