LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem displaying Bash array values. (https://www.linuxquestions.org/questions/programming-9/problem-displaying-bash-array-values-265235/)

shinobi59 12-11-2004 10:51 AM

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

bigearsbilly 12-20-2004 07:21 AM

what about ?

Quote:


eval echo "RECORD${ROW}[$x]"


bigearsbilly 12-20-2004 08:02 AM

correction

Code:


eval    RECORD=\${RECORD${ROW}[$x]}


shinobi59 01-17-2006 05:45 PM

Well home at last
 
Ok... so it's been a while since I got back to this thread. :o

I've been in a bit of life turmoil ... as I was working some extreme hours 100+ and then decided another job was in order so I now find myself in Richardson, TX. :)

Thanks for your answers and I will give them a try once I find my data files again. I am still sorting out from the move. I will let you know if that works or not ... Thanks again. :D


All times are GMT -5. The time now is 07:18 AM.