使用linux 2.6.18 ..
/bin/bash -version
GNU bash, version 1.14.7(1)
(snmpwalk命令在控制台中提供输出,如下面的......)
33
4232
5342
632
45342
23342
3243
我使用shell脚本从snmp中grep数据并尝试将其添加到数组中。但是不能用以下问题做到这一点......
#!/bin/bash
OID="1.3.6.1.2.1.2.2.1"
declare -a index=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.1)
declare -a upload=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.10)
declare -a download=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.11)
echo "retrieved ${#index[@]} elements"
echo "#${index[3]}: up=${upload[2]} down=${download[2]}"
输出:
+ declare -a index=1 2
j2.sh: 4: j2.sh: declare: not found
(所以我不能使用'declare')
第二路
#!/bin/bash
OUTPUT1=$(snmpbulkwalk -Oqv -c public -v 2c localhost
1.3.6.1.4.1.21067.4.1.1.1 2> /dev/null)
i=1
index=1
upload=1
download=1
for LINE in ${OUTPUT1}
do
index=$LINE
OUTPUT2=$(snmpget -Oqv -c public -v 2c localhost 1.3.6.1.4.1.21067.4.1.1.10.$i 2> /dev/null)
OUTPUT3=$(snmpget -Oqv -c public -v 2c localhost 1.3.6.1.4.1.21067.4.1.1.11.$i 2> /dev/null)
upload=$OUTPUT2
download=$OUTPUT3
i=`expr $i + 1`
done
它给了bettelnack大量的数据......
3ra approch:
#!/bin/bash
OUTPUT=$(snmpbulkwalk -Oqv -c public -v 2c localhost
1.3.6.1.4.1.21067.4.1.1.1 2> /dev/null)
i=0
index[3000]=0
for LINE in ${OUTPUT} ; do
index[$i]=$LINE
i=`expr $i + 1`
done
这里索引[1],索引[2] ......不是fount
答案 0 :(得分:1)
declare -a index=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.1) declare -a upload=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.10) declare -a download=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.11)
你为什么要在一条线上做这一切?在单独的行上执行此操作,它应该可以正常工作。
declare -a index=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.1)
declare -a upload=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.10)
declare -a download=$(snmpwalk -v 2c -c public -Oqv localhost ${OID}.11)
如果这不起作用,您使用的是什么版本的Bash?你从/bin/bash --version
获得了什么?