Brick
Brick

Reputation: 43

Bash array of strings

I have a bash script which gathers snmp information and searches it for key phrases to alert on.

I use an snmp command to output logs from a network device, this is an example of the code and the original output-

/usr/bin/snmpwalk -v 2c -c public $HOST .1.3.6.1.3.94.1.11.1.9.32.128.0.192.255

SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.251 = STRING: "Host link down. (port: 1)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.252 = STRING: "Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.253 = STRING: "A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.254 = STRING: "A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
SNMPv2-SMI::experimental.94.1.11.1.9.32.128.0.192.255.27.228.239.0.0.0.0.0.0.0.0.255 = STRING: "Host link down. (port: 1)"

In my bash script I create an array from this and strip out the bumph at the start-

declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))

This creates this-

"Host link down. (port: 1)"
"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"
"A scrub-vdisk job was started. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"A scrub-vdisk job completed. No errors were found. (vdisk: test, SN: 00c0ff1bf86d0000a6c09c5600000000)"
"Host link down. (port: 1)"

My problem is that this array treats every word as a separate entry, so my actual output is this-

"Host 
link 
down. 
(port: 
1)"

So whenever I search for individual words it works fine, but when I search for phrases it breaks.

Ideally what I would like it to do is store each line of text as its own entry in the array, like this-

declare -a LOG=('"Host link down. (port: 1)"' '"Host link up. (port: 1, speed: 8 Gbps, number of loop ID(s):"') etc

I'm just not sure how to manipulate the original declare line to do this...

Here is all my code-

HOST="10.10.10.10"
OID=".1.3.6.1.3.94.1.11.1.9.32.128.0.192.255"
declare -a CRITICAL=('errors' 'An event was reported by a disk drive')

declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))

for RESULT in `echo ${LOG[*]}`
    do
    for WORD in `echo ${CRITICAL[*]}`
        do
            case "$RESULT" in
                *$WORD*)
                    OUTPUT+="$RESULT -CRITICAL "
                    ;;
                *)
                    OUTPUT+="$RESULT "
            esac    

        done
done

Upvotes: 3

Views: 285

Answers (1)

cmh
cmh

Reputation: 10937

If you set the IFS (internal field separator) variable to \n it should achieve what you want.

IFS=$'\n' declare -a LOG=($(/usr/bin/snmpwalk -v 2c -c public $HOST $OID | cut -d" " -f 4-))

There is also an issue with the way you are iterating through the results. try the following so you don't split the individual lines into words (which the * operator will do).

for RESULT in "${LOG[@]}"
    do
    for WORD in "${CRITICAL[@]}"
        do

Upvotes: 2

Related Questions