Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
Showing results for 
Search instead for 
Did you mean: 

adding elements to an array

Chris Vidal
Occasional Visitor

adding elements to an array

snmpwalk -c xx yy system generates the following.

cvidal@cayd04-mds1:~> snmpwalk -c tmedomain4 system
sysDescr.0 = STRING: Cisco IOS Software, 3800 Software (C3845-ADVIPSERVICESK9-M), Version 12.4(3b), RELEASE SOFTWARE (fc3)
Technical Support:
Copyright (c) 1986-2005 by Cisco Systems, Inc.
Compiled Fri 09-Dec-05 09:08 by alnguyen
sysObjectID.0 = OID: enterprises.9.1.544
sysUpTime.0 = Timeticks: (28557767) 3 days, 7:19:37.67
sysContact.0 = STRING: MCI 800-256-9284
sysName.0 = STRING: merv-m008napa-687103
sysLocation.0 = STRING:
sysServices.0 = INTEGER: 78
sysORLastChange.0 = Timeticks: (0) 0:00:00.00

I would like to put each line into an array element as I receive the response for later printing or assigning to vars

set -A ARRAY just setsup a single element array.

Honored Contributor

Re: adding elements to an array

>I would like to put each line into an array element as I receive the response for >later printing or assigning to vars

# snmpwalk -c xx yy system | awk '{l[NR]=$0}'
A. Clay Stephenson
Acclaimed Contributor

Re: adding elements to an array

Here's a simple example using the output of ls -l /tmp but it should give you the idea:


typeset PROG=${0}
typeset -i MAXARRAY=1023
typeset -i KNT=0
typeset X=""
typeset -i STAT=0

#your snmpwalk will replace the ls -l

ls -l /tmp | while read X
typeset A[${KNT}]="${X}"
((KNT += 1))
if [[ ${KNT} -gt ${MAXARRAY} ]]
echo "${PROG}: Missing elements" >&2
# Now let's read 'em out
typeset -i I=0
while [[ ${I} -lt ${KNT} ]]
echo "${I} -> \"${A[${I}]}\""
((I += 1))
exit ${STAT}


Note that shell arrays are limited to a maximum of 1024 elements so you might want to rethink your problem (Perl?).
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: adding elements to an array

You could maybe try something like

typeset -i i=-1; set -A SNMPWALK
/your/snmpwalk_cmd 2>/dev/null \
| while read line; do

But beware not to start your snmpwalk too high in the tree because afaik a shell array can only hold up to 1024 elements.
Besides, because of wasteful memory usage
dumping large amounts of output in arrays is not considered terribly efficient programming.

In Perl it is much easier to store each output line as an array element,
and there you are only restricted by your process'es memory share.
But for efficiency's sake one shouldn't store it in an array.


my @SNMPWALK = qx(/your/snmpwalk_cmd);

or if you want more control

local *SNMP_PIPE
my $child = open SNMP_PIPE, '-|';
die "Cannot open pipe to snmpwalk" if !defined $child;
if ($child) {
close SNMP_PIPE;
} else {
local %ENV; # if taint mode demands
exec qw(/path/to/snmpwalk arg1 arg2 arg3);
# maybe you want to chomp new lines
Madness, thy name is system administration
Chris Vidal
Occasional Visitor

Re: adding elements to an array