Bash 字符串数组
Bash array of strings
我有一个 bash 脚本,它收集 snmp 信息并搜索关键短语以发出警报。
我使用snmp命令从网络设备输出日志,这是代码示例和原始输出-
/usr/bin/snmpwalk -v 2c -c public $HOST .1.3.6.1.3.94.1.11.1.9.32.128.0.192.255
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.251 = STRING: "Host link down. (port: 1)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.252 = STRING: "Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.253 = STRING: "A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.254 = STRING: "A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.255 = STRING: "Host link down. (port: 1)"
在我的 bash 脚本中,我从中创建了一个数组并去掉了开始处的凸起-
declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
这创建了这个-
"Host link down. (port: 1)"
"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
"A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"Host link down. (port: 1)"
我的问题是这个数组将每个单词都视为一个单独的条目,所以我的实际输出是这样的-
"Host
link
down.
(port:
1)"
因此,每当我搜索单个单词时,它都能正常工作,但当我搜索短语时,它就会中断。
理想情况下,我希望它做的是将每一行文本作为其自己的条目存储在数组中,就像这样-
declare -a LOG=('"Host link down. (port: 1)"' '"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"') etc
我只是不确定如何操纵原始声明行来执行此操作...
这是我所有的代码-
HOST="10.10.10.10"
OID=".1.3.6.1.3.94.1.11.1.9.32.128.0.192.255"
declare -a CRITICAL=('errors' 'An event was reported by a disk drive')
declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
for RESULT in `echo ${LOG[*]}`
do
for WORD in `echo ${CRITICAL[*]}`
do
case "$RESULT" in
*$WORD*)
OUTPUT+="$RESULT -CRITICAL "
;;
*)
OUTPUT+="$RESULT "
esac
done
done
如果您将 IFS (internal field separator) 变量设置为 \n
,它应该会达到您想要的效果。
IFS=$'\n' declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
您迭代结果的方式也存在问题。尝试以下操作,这样您就不会将各个行拆分为单词(* 运算符会这样做)。
for RESULT in "${LOG[@]}"
do
for WORD in "${CRITICAL[@]}"
do
我有一个 bash 脚本,它收集 snmp 信息并搜索关键短语以发出警报。
我使用snmp命令从网络设备输出日志,这是代码示例和原始输出-
/usr/bin/snmpwalk -v 2c -c public $HOST .1.3.6.1.3.94.1.11.1.9.32.128.0.192.255
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.251 = STRING: "Host link down. (port: 1)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.252 = STRING: "Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.253 = STRING: "A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.254 = STRING: "A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.255 = STRING: "Host link down. (port: 1)"
在我的 bash 脚本中,我从中创建了一个数组并去掉了开始处的凸起-
declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
这创建了这个-
"Host link down. (port: 1)"
"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
"A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"Host link down. (port: 1)"
我的问题是这个数组将每个单词都视为一个单独的条目,所以我的实际输出是这样的-
"Host
link
down.
(port:
1)"
因此,每当我搜索单个单词时,它都能正常工作,但当我搜索短语时,它就会中断。
理想情况下,我希望它做的是将每一行文本作为其自己的条目存储在数组中,就像这样-
declare -a LOG=('"Host link down. (port: 1)"' '"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"') etc
我只是不确定如何操纵原始声明行来执行此操作...
这是我所有的代码-
HOST="10.10.10.10"
OID=".1.3.6.1.3.94.1.11.1.9.32.128.0.192.255"
declare -a CRITICAL=('errors' 'An event was reported by a disk drive')
declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
for RESULT in `echo ${LOG[*]}`
do
for WORD in `echo ${CRITICAL[*]}`
do
case "$RESULT" in
*$WORD*)
OUTPUT+="$RESULT -CRITICAL "
;;
*)
OUTPUT+="$RESULT "
esac
done
done
如果您将 IFS (internal field separator) 变量设置为 \n
,它应该会达到您想要的效果。
IFS=$'\n' declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))
您迭代结果的方式也存在问题。尝试以下操作,这样您就不会将各个行拆分为单词(* 运算符会这样做)。
for RESULT in "${LOG[@]}"
do
for WORD in "${CRITICAL[@]}"
do