0

我的 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)

4

3 回答 3

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
于 2013-09-16T15:57:28.397 回答
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在循环内部进行,并在此处文档的第一列中放置一个正则表达式,或者只是端口号。

于 2013-09-16T16:59:00.423 回答
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
于 2013-09-16T16:06:28.210 回答