我試圖讓SNMP走進一個數組,變量中的數據格式爲"data1 data2"
"data3 data4"
。基本上我只是希望雙引號之間的數據,不管有多少空格。如何從命令變量在bash中創建數組?
我一直在谷歌約一個半小時,無法弄清楚如何獲得正確格式的數組。
這裏是我現在所擁有的代碼的SNIPPIT ...
IN=$(snmpwalk -Oqv -v2c -c$community $ipaddr .1.3.6.1.4.1.32050.2.1.26.2)
portdesc=($(echo $IN))
這並把它放在一個陣列,但在嘗試訪問第一個數據集時,我得到"data1
而不是"data1 data2"
。我可以欺騙並使用兩個變量,但我寧願按照正確的方式進行操作,只需引用陣列中的項目即可。 如果你能幫助我,我將永遠是偉大的。
echo "DEBUG0: ${portdesc[@]}"
echo "DEBUG1: ${portdesc[0]}"
DEBUG0: "Relay Output" "Expansion Power" "Expansion Tripped" "Switch Input" "Radio 1 Power" "Radio 2 Power" "Radio 3 Power" "Radio 4 Power" "Radio 1 Sync" "Radio 2 Sync" "Radio 3 Sync" "Radio 4 Sync" "Radio 1 Tripped" "Radio 2 Tripped" "Radio 3 Tripped" "Radio 4 Tripped" "SyncPipe Power" "SyncPipe Tripped" "Switch to NMEA" "2D Fix" "3D Fix" "DGPS Fix" "1PPS Active" "Radio 1 Power" "Radio 2 Power" "Radio 3 Power" "Radio 4 Power" "Radio 1 Sync" "Radio 2 Sync" "Radio 3 Sync" "Radio 4 Sync" "Radio 1 Tripped" "Radio 2 Tripped" "Radio 3 Tripped" "Radio 4 Tripped" "SyncPipe Power" "SyncPipe Tripped" "Switch to NMEA" "2D Fix" "3D Fix" "DGPS Fix" "1PPS Active" "Radio 1 Power" "Radio 2 Power" "Radio 3 Power" "Radio 4 Power" "Radio 1 Sync" "Radio 2 Sync" "Radio 3 Sync" "Radio 4 Sync" "Radio 1 Tripped" "Radio 2 Tripped" "Radio 3 Tripped" "Radio 4 Tripped" "SyncPipe Power" "SyncPipe Tripped" "Switch to NMEA" "2D Fix" "3D Fix" "DGPS Fix" "1PPS Active"
DEBUG1: "Relay
SNMP命令將從CLI
$ snmpwalk -O qv -v2c -c<community> <ip> .1.3.6.1.4.1.32050.2.1.26.2
"Relay Output"
"Expansion Power"
"Expansion Tripped"
"Switch Input"
"Radio 1 Power"
"Radio 2 Power"
"Radio 3 Power"
"Radio 4 Power"
"Radio 1 Sync"
"Radio 2 Sync"
"Radio 3 Sync"
"Radio 4 Sync"
"Radio 1 Tripped"
"Radio 2 Tripped"
查看原文職位SNMP輸出 – Chris 2012-01-05 23:47:24
@克里斯:我所有請參閱解析它的結果。我認爲這與它被換行符分隔是一致的。如果你發佈實際的示例輸入,它會很有幫助(假設我還沒有回答你的問題)。 – mrj 2012-01-05 23:49:30
花了我一分鐘來弄清楚你在問什麼。再次編輯原始文章,這次可能有用的信息。 :) – Chris 2012-01-05 23:54:36