简体   繁体   English

如何从命令变量在bash中创建数组?

[英]How do I create an array in bash from a command variable?

I'm trying to get an SNMP walk into an array, the data in the variable is in the format "data1 data2" "data3 data4" . 我正在尝试使SNMP进入数组,变量中的数据格式为"data1 data2" "data3 data4" Essentially I just want the data between the double quotes, regardless of how many spaces there may be. 本质上,我只希望双引号之间的数据,而不管可能有多少空格。

I've been googleing for about an hour and a half and cannot figure out how to get the array correctly formatted. 我一直在谷歌搜索约一个半小时,无法弄清楚如何正确格式化数组。

Here is a snippit of the code that I have right now... 这是我现在拥有的代码片段...

IN=$(snmpwalk -Oqv -v2c -c$community $ipaddr .1.3.6.1.4.1.32050.2.1.26.2)
portdesc=($(echo $IN))

This does put it in an array, but when trying to access the first data set I get "data1 instead of "data1 data2" . I can cheat and just use two variables but I'd rather do it the right way and just reference the array for the item that I want. If you can help me with this I will be eternally greatfull. 这确实将其放置在数组中,但是当尝试访问第一个数据集时,我得到"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 COMMAND FROM CLI 来自CLI的SNMP命令

$ 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"

I'd be more confident about this if you'd given example input (I don't know what the output of snmpwalk looks like). 如果您提供示例输入,我对此会更有信心(我不知道snmpwalk的输出是什么样的)。 But from what I understand, you're expecting the array to split the text only at newlines, not at spaces. 但是据我了解,您期​​望数组仅在换行符处而不是空格处分割文本。 By default, bash splits at spaces, tabs, and newlines. 默认情况下,bash在空格,制表符和换行符处分割。 To change that behavior, you can modify the value of IFS ( I nput F ield S eparator): 要改变这种行为,你可以修改的值IFS NPUT˚Field 小号 eparator):

OLDIFS="$IFS"
IFS=$'\n'      # newlines are the only separator
IN=($(my_command ...))
IFS="$OLDIFS"

The extra two lines save and restore the value of IFS, in case other parts of your script depend on it. 如果脚本的其他部分依赖IFS的值,则额外的两行会保存和恢复IFS的值。

(Incidentally, if you google "bash array newlines" you find some things explaining this.) (顺便说一句,如果您用Google搜索“ bash数组换行符”,您会发现一些解释这一点的方法。)

Try something like this: 尝试这样的事情:

mlm@matt-mmf-macbook.local:~
$ a=( "data1 data2" "data3 data4" )

mlm@matt-mmf-macbook.local:~
$ echo "${a[0]}"
data1 data2

mlm@matt-mmf-macbook.local:~
$ echo "${a[1]}"
data3 data4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM