Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays BASH将值存储在数组中,并检查每个值的差异_Arrays_Shell_Variables - Fatal编程技术网

Arrays BASH将值存储在数组中,并检查每个值的差异

Arrays BASH将值存储在数组中,并检查每个值的差异,arrays,shell,variables,Arrays,Shell,Variables,[CentOS、BASH、cron]是否有一种方法来声明即使在系统重新启动时也会保留的变体 场景是snmpwalk接口I/O错误并将值存储在数组中。再次执行snmpwalk的cron作业(比如5分钟后)将具有另一组值。我想将它们与每个接口先前对应的值进行比较。如果差值超过阈值50,将生成警报 所以问题是:如何存储在系统中丢失的数组变量?如何检查两个数组中每个值的差异 更新2012年3月16日我在此附上我的最终脚本供您参考 #!/bin/bash # This script is to monit

[CentOS、BASH、cron]是否有一种方法来声明即使在系统重新启动时也会保留的变体

场景是snmpwalk接口I/O错误并将值存储在数组中。再次执行snmpwalk的cron作业(比如5分钟后)将具有另一组值。我想将它们与每个接口先前对应的值进行比较。如果差值超过阈值50,将生成警报

所以问题是:如何存储在系统中丢失的数组变量?如何检查两个数组中每个值的差异

更新2012年3月16日我在此附上我的最终脚本供您参考

#!/bin/bash
# This script is to monitor interface Input/Output Errors of Cisco devices, by snmpwalk the error values every 5 mins, and send email alert if incremental value exceeds threshold (e.g. 500).
# Author: Wu Yajun | Created: 12Mar2012 | Updated: 16Mar2012
##########################################################################

DIR="$( cd "$( dirname "$0" )" && pwd )"
host=device.ip.addr.here

# Check and initiate .log file storing previous values, create .tmp file storing current values.
test -e $DIR/host1_ifInErrors.log || snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.log
snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.tmp

# Compare differences of the error values, and alert if diff exceeds threshold.
# To exclude checking some interfaces, e.g. Fa0/6, Fa0/10, Fa0/11, change the below "for loop" to style as:
# for i in {1..6} {8..10} {13..26}
totalIfNumber=$(echo $(wc -l $DIR/host1_ifInErrors.tmp) | sed 's/ \/root.*$//g')

for (( i=1; i<=$totalIfNumber; i++))
do
        currentValue=$(cat $DIR/host1_ifInErrors.tmp | sed -n ''$i'p' | sed 's/^.*Counter32: //g')
        previousValue=$(cat $DIR/host1_ifInErrors.log | sed -n ''$i'p' | sed 's/^.*Counter32: //g')
        diff=$(($currentValue-$previousValue))
        [ $diff -ge 500 ] && (ifName=$(echo $(snmpwalk -c public -v 1 $host IF-MIB::ifName.$i) | sed 's/^.*STRING: //g') ; echo "ATTENTION - Input Error detected from host1 interface $ifName" | mutt -s "ATTENTION - Input Error detected from host1 interface $ifName" <email address here>)
done

# Store current values for next time checking.
snmpwalk -c public -v 1 $host IF-MIB::ifInErrors > $DIR/host1_ifInErrors.log

将变量保存在文件中。添加日期戳:

echo "$(date)#... variables here ...." >> "$file"
从文件中读取最后一个值:

tail -1 "$file" | cut "-d#" -f2 | read ... variables here ....
这也为您提供了一个很好的日志文件,您可以在其中监视更改。我建议总是附加到文件中,这样您就可以很容易地看到服务何时因某种原因关闭/未运行

要检查更改,可以使用简单的if


SQL数据库是另一种选择,它以额外的复杂性为代价为您提供更多功能。但最终,您需要以某种方式将数据保存到磁盘。如果使用文本文件或SQL数据文件,这是一个设计问题。至于我的任务,实际上上次更新的值和当前值之间的差异很重要。我仍然在寻找如何声明可以保留在系统中的变量的答案。我听说过环境变量和局部变量之类的东西,但不确定里面是什么。感谢您的建议。我在第一句中回答:将变量保存在文件中变量总是只保存在内存中。环境变量导出到子进程,而本地变量则不导出。没有变量能在创建它的过程结束后幸存下来。我明白了,所以值必须保存在本地的某个地方。我会选择日志文件和SQLite。谢谢
if [[ "...old values..." != "...new values..." ]]; then
    send mail
fi