Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array thats not working

Status
Not open for further replies.

sumncguy

IS-IT--Management
May 22, 2006
118
US
The script
#! /bin/ksh
#
set -x
n=0
snmpwalk -c xxxxx yyyyy system | while IFS=read line
do
arrayname[$n]=$line
n=$(( $n + 1 ))
echo $(arrayname[0])
done

The a portion of the output w/set -x enabled
+ n=0
+ snmpwalk -c tmedomain4 192.168.63.247 system
+ line
+ IFS=read
sysDescr.0 = STRING: Cisco IOS Software, 3800 Software (C3845-ADVIPSERVICESK9-M), Version 12.4(3b), RELEASE SOFTWARE (fc3)
+ arrayname[0]=
+ n=1
+ arrayname[0]
ter[9]: arrayname[0]: not found
+ echo

+ line
+ IFS=read
Technical Support: + arrayname[1]=
+ n=2
+ arrayname[0]
ter[9]: arrayname[0]: not found
+ echo


I need to learn how to use arrays. It would make my life alot easier. But I just cant see what Im doing wrong here.

I want to put the contents of the snmp response into an array, then be able to print or assign its value to a variable.

SunOS xxxxx 5.8 Generic_108528-15 sun4u sparc SUNW,UltraSPARC-IIi-cEngine and running the script in KSH.

Can someone show me the light ?

Thanks
 
What about:

Code:
#!/bin/ksh

n=0
set -A arrayname `snmpwalk -c xxxxx yyyyy system`

while [ ${n} -le ${#arrayname[@]} ]
do
   echo ${arrayname[${n}]}
   ((n=n+1))
done

Regards,
Chuck
 
Sorry, but I should of included quotes around the snmpwalk command like this:

Code:
#!/bin/ksh

n=0
set -A arrayname [COLOR=#ff0000][b]"[/b][/color]`snmpwalk -c xxxxx yyyyy system`[COLOR=#ff0000][b]"[/b][/color]

while [ ${n} -le ${#arrayname[@]} ]
do
   echo ${arrayname[${n}]}
   ((n=n+1))
done

Regards,
Chuck
 
...and one last thing. I should have -lt instead of -le like this:

Code:
#!/bin/ksh

n=0
set -A arrayname "`snmpwalk -c xxxxx yyyyy system`"

while [ ${n} [COLOR=red][b]-lt[/b][/color] ${#arrayname[@]} ]
do
   echo ${arrayname[${n}]}
   ((n=n+1))
done

Regards,
Chuck
 
I think I will go get my morning coffee before I post anymore... [morning]

Regards,
Chuck
 
You can shorten some things like:
${arrayname[${n}]} cam be ${arrayname[n]}

and

((n=n+1)) can be ((n+=1))

# set -A arrayname $(ls /tmp) might give you 30 files.
# arrayNum=${#arrayname[@]} will give you the number of files.
# ${#arrayname[2]} will give you the length of the 3rd filename in the array. Count starts at zero.

${arrayname[@]} is the same as "1" "2" "3"
while
${arrayname[*]} is the same as "1 2 3"

Pick up the O'Reilly "Learning the Korn Shell" book. It addresses everything you need in programming the korn shell.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top