2

I'm trying to get an SNMP walk into an array, the data in the variable is in the format "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.

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 Answers 2

7

I'd be more confident about this if you'd given example input (I don't know what the output of snmpwalk looks like). 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. To change that behavior, you can modify the value of IFS (Input Field Separator):

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.

(Incidentally, if you google "bash array newlines" you find some things explaining this.)

Sign up to request clarification or add additional context in comments.

4 Comments

See original post for SNMP output
@Chris: All I see is the result of your parsing of it. It's consistent with it having been newline-delimited, I think. If you post actual example input it'd be helpful (assuming I haven't already answered your question).
Took me a minute to figure out what you were asking for.. Edited the orig post again, this time with possible useful information. :)
@Chris: Ah, cool. Since I seem to have guessed right, the input will just serve to help others who see the question later!
0

Try something like this:

[email protected]:~
$ a=( "data1 data2" "data3 data4" )

[email protected]:~
$ echo "${a[0]}"
data1 data2

[email protected]:~
$ echo "${a[1]}"
data3 data4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.