LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2004, 10:51 AM   #1
shinobi59
Member
 
Registered: Oct 2004
Location: Dimension X
Distribution: All
Posts: 60

Rep: Reputation: 15
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
 
Old 12-20-2004, 07:21 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,484

Rep: Reputation: 223Reputation: 223Reputation: 223
what about ?

Quote:

eval echo "RECORD${ROW}[$x]"
 
Old 12-20-2004, 08:02 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,484

Rep: Reputation: 223Reputation: 223Reputation: 223
correction

Code:
eval    RECORD=\${RECORD${ROW}[$x]}
 
Old 01-17-2006, 05:45 PM   #4
shinobi59
Member
 
Registered: Oct 2004
Location: Dimension X
Distribution: All
Posts: 60

Original Poster
Rep: Reputation: 15
Well home at last

Ok... so it's been a while since I got back to this thread.

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM
Simple bash problem - checking for files and displaying the first line morrolan Programming 4 03-07-2005 10:40 AM
JAVA: displaying largest number of an array bug randomx Programming 17 06-28-2004 07:59 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration