- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: adding elements to an array
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2006 09:55 AM
тАО07-13-2006 09:55 AM
adding elements to an array
cvidal@cayd04-mds1:~> snmpwalk -c tmedomain4 192.168.63.247 system
sysDescr.0 = STRING: Cisco IOS Software, 3800 Software (C3845-ADVIPSERVICESK9-M), Version 12.4(3b), RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
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.
tks
- Tags:
- ARRAY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2006 10:19 AM
тАО07-13-2006 10:19 AM
Re: adding elements to an array
# snmpwalk -c xx yy system | awk '{l[NR]=$0}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2006 10:30 AM
тАО07-13-2006 10:30 AM
Re: adding elements to an array
#!/usr/bin/sh
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
do
typeset A[${KNT}]="${X}"
((KNT += 1))
if [[ ${KNT} -gt ${MAXARRAY} ]]
then
STAT=255
echo "${PROG}: Missing elements" >&2
break
fi
done
# Now let's read 'em out
typeset -i I=0
while [[ ${I} -lt ${KNT} ]]
do
echo "${I} -> \"${A[${I}]}\""
((I += 1))
done
exit ${STAT}
----------------------------
Note that shell arrays are limited to a maximum of 1024 elements so you might want to rethink your problem (Perl?).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2006 07:18 PM
тАО07-13-2006 07:18 PM
Re: adding elements to an array
typeset -i i=-1; set -A SNMPWALK
/your/snmpwalk_cmd 2>/dev/null \
| while read line; do
SNMPWALK[$((i+=1))]="$line"
done
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.
e.g.
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) {
@SNMPWALK =
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
chomp @SNMPWALK
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2006 04:46 AM
тАО07-14-2006 04:46 AM