How to create an array in bash from the command line?

I am trying to get an SNMP transition to an array, the data in the variable is in the format "data1 data2" "data3 data4" . Essentially, I just need the data between double quotes, no matter how many spaces there may be.

I worked for about an hour and a half and cannot figure out how to format the array correctly.

Here is a piece of 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 puts it in an array, but when I try to access the first data set, I get "data1 instead of "data1 data2" . "data1 data2" I can trick and just use two variables, but I would rather do it right and just reference the array for the element that I I want, if you can help me, I will be forever magnificent.

 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

 $ 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" 
+2
source share
2 answers

I would be more confident about this if you provided an example input (I don't know what snmpwalk output looks like). But from what I understand, you expect the array to split the text only on new lines, not spaces. By default, bash is broken into spaces, tabs, and newlines. To change this behavior, you can change the value of IFS ( I nput F ield S eparator):

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

The additional two lines save and restore the IFS value if the other parts of your script depend on it.

(By the way, if you google "bash array newlines", you will find some things explaining this.)

+6
source

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 
0
source

All Articles