LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 10-03-2008, 02:57 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
how to load an array from a file in RHEL bash script


I want to load an array from a file. I developed the following:

declare -a targetarray
count=0

while read line
do
targetarray[$count]=$line
((count++))
done < /tmp/find-fmfm-reachlist-sorted.txt

When I echo $line, it shows each line in the file, as expected. An echo of $count shows it increasing. However, an echo of $targetarray[$count] shows it storing the first value of $line in each row in the array. What am I doing wrong?

line = 2143347423 9038945094 130
targetarray = 2143347423 9038945094 130[0]
line = 2147292028 9034514086 130
targetarray = 2143347423 9038945094 130[1]
line = 2148080083 9034514086 130
targetarray = 2143347423 9038945094 130[2]
.
.
.
 
Old 10-03-2008, 04:08 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
It should work! How did you echo the array elements? Which command?
 
Old 10-03-2008, 04:18 PM   #3
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Thanks! I added the echo commands back into the script, and an if statement to stop it:

while read line
do
echo "count = $count"
echo "line = $line"
targetarray[$count]=$line
echo "targetarray row = $targetarray[$count]"
((count++))
if [ $count -gt 5 ]
then
exit
fi
done < /tmp/find-fmfm-reachlist-sorted.txt

output:
count = 0
line = 2143347423 9038945094 130
targetarray row = 2143347423 9038945094 130[0]
count = 1
line = 2147292028 9034514086 130
targetarray row = 2143347423 9038945094 130[1]
count = 2
line = 2148080083 9034514086 130
targetarray row = 2143347423 9038945094 130[2]
count = 3
line = 2149126551 9034515033 130
targetarray row = 2143347423 9038945094 130[3]
count = 4
line = 2392184778 2394951912 130
targetarray row = 2143347423 9038945094 130[4]
count = 5
line = 2392184779 2394951912 130
targetarray row = 2143347423 9038945094 130[5]
 
Old 10-03-2008, 04:24 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You forgot the curly brackets:
Code:
echo "targetarray row = ${targetarray[$count]}"
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 02:16 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 05:37 AM
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 09:20 AM
Bash script - how to reassemble file from array sixerjman Programming 11 08-25-2006 12:45 PM
Bash script text line into an array toolshed Programming 1 06-13-2005 06:49 PM


All times are GMT -5. The time now is 01:50 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration