Member
Registered: Oct 2004
Location: Dimension X
Distribution: All
Posts: 60
Rep:
|
Problem displaying Bash array values.
So How does one create a new thread on this forum?
I have a problem with a bash script.
I am trying to do something in which I kinda need to use a multi-dimensional array only Bash doesn't do that ... at least not that I am aware of.
I am trying to separate a line of field names and count the fields which I have managed to do. I can also add these fields to an array. Now I want to get the length of the fields only I am getting errors not from the length operator # from my code.
Here are the field names in the header of the flat file.
"Host Name","Serial Number","Asset Number","BarCode Number","Server Description","Suite ID","Suite ID 2","App ID","O/S ID","License","Production/non","Server use","Server type id","Service Pack","Deployed","Purchased","Vendor id","Contract.","Contract Expires","Support ID","Model","Processor","Memory","drive id","Swap Space","Drive Description","Fiber Port 1","Switch/Port1","Fiber Port 2","Switch/Port2","Fiber Port 3","Switch/Port3","Fiber Port 4","Switch/Port4","Fiber Port 5","Switch/Port5","Fiber Port 6","Switch/Port 6","Tape Drive Description","nic id","Repair","Zip Drive","Jaz Drive","Tape Drive","DHCP","Snmp","WatchFull","Insight","Other Monitor","NetView","TEC (Tivoli)","MN","EP","Power/Plug","Power Circuit","Hardware Maint","Support Line","Day of the week","Reboot Time","Primary DNS","Backup DNS","Gateway IP","Admin","Admin Phone","Admin Notification","Contact","Contact Phone","Contact Notification","Rack","Location ID","Location City","Location State","Location Zip","Critical","Notes","System Log","System Specs","PinX","PinY"
I can strip out the quotation marks leaving a comma separated values (CSV).
head -1 <file> | tr -d "\042"
Host Name,Serial Number,Asset Number,BarCode Number,Server Description,Suite ID,Suite ID 2,App ID,O/S ID,License,Production/non,Server use,Server type id,Service Pack,Deployed,Purchased,Vendor id,Contract.,Contract Expires,Support ID,Model,Processor,Memory,drive id,Swap Space,Drive Description,Fiber Port 1,Switch/Port1,Fiber Port 2,Switch/Port2,Fiber Port 3,Switch/Port3,Fiber Port 4,Switch/Port4,Fiber Port 5,Switch/Port5,Fiber Port 6,Switch/Port 6,Tape Drive Description,nic id,Repair,Zip Drive,Jaz Drive,Tape Drive,DHCP,Snmp,WatchFull,Insight,Other Monitor,NetView,TEC (Tivoli),MN,EP,Power/Plug,Power Circuit,Hardware Maint,Support Line,Day of the week,Reboot Time,Primary DNS,Backup DNS,Gateway IP,Admin,Admin Phone,Admin Notification,Contact,Contact Phone,Contact Notification,Rack,Location ID,Location City,Location State,Location Zip,Critical,Notes,System Log,System Specs,PinX,PinY
Now if I use this function I can build an array:
build_array()
{
# Prerequisite is to run NOF() and NROWS() and provide those to this function
# $1 = TCOLS $2 = TROWS $3 = DB Flat File
for ROW in $(seq $2)
do
ROW=$(($ROW))
line="$(sed -n ${ROW}p ${3})"
for x in $(seq $1)
do
x=$(($x))
RECORD=RECORD${ROW}[$x]
echo $RECORD
eval "RECORD${ROW}[$x]"=$(echo $line|cut -d, -f${x})
done
done
}
This all works fine. Now I need to echo "{#RECORD${ROW}[$x]}" in order to get the field length.
So I tried just echoing the fiield first with:
echo "RECORD${ROW}[$x]"
But I get errors:
(Loading the array)
++ cut -d, -f19
+ eval 'RECORD1[19]="Contract' 'Expires"'
RECORD1[19]="Contract Expires"
++ RECORD1[19]=Contract Expires
(Display the array)
+ echo 'RECORD{1}[19]'
RECORD{1}[19]
As you can see loading the array works ...
But the echo does not print the value of the array RECORD1[19].
Any ideas?
Shinobi
__________________
-- Shinobi59
|