LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 01:33 PM   #1
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Rep: Reputation: 16
Question problem with arrays in bash


Hi everybody, I have a problem with the bash script (see below)

I want to write contents of the log file into an array
with one line per element.

======================================================================

Code:
declare -a myarray

let count=0
tail -n20 "myfile.log" | while read tmpline
do
  myarray[$count]="$tmpline"
  let count=count+1
done

echo "N. of elements:"${#myarray[@]}

echo "Array contents:"${myarray[@]}
======================================================================

and as a result I get zero elements and empty array.
(myfile.log is not empty)

What am I doing wrong?

Last edited by igor.R; 04-17-2009 at 01:37 PM.
 
Old 04-17-2009, 07:59 PM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
The problem is that when you use a pipe you create a subprocess. So the while loop is running as a subprocess and the array elements you assign never get "seen" by the parent. You can have the while loop run in the same process as the rest of the script by using redirection like so:

Code:
while read variable list; do
# insert  body of loop here
done < file
There is a capability in bash called Process Substitution (see the man page) which I thought should remove the necessity of a creating a temporary file just to redirect into the loop. But I have never been able to make it work. (If you or somebody else can explain how to use that with a while loop, I'd love to hear it.)

Last edited by blackhole54; 04-17-2009 at 08:01 PM.
 
Old 04-17-2009, 08:12 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
Or you could use:
Code:
for tmpline in $(tail -n20 "myfile.log")
do
...
Sorry - blackhole 54 pointed out my obvious mistake below

Last edited by Robhogg; 04-20-2009 at 12:50 PM. Reason: Admission of error
 
Old 04-17-2009, 08:21 PM   #4
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
@Robhogg

Wouldn't spaces in the contents of myfile.log cause problems? I.e. each "word" would be separate instance of $tmpline?
 
Old 04-17-2009, 09:05 PM   #5
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by igor.R View Post
Hi everybody, I have a problem with the bash script (see below)

I want to write contents of the log file into an array
with one line per element.

======================================================================

Code:
declare -a myarray

let count=0
tail -n20 "myfile.log" | while read tmpline
do
  myarray[$count]="$tmpline"
  let count=count+1
done

echo "N. of elements:"${#myarray[@]}

echo "Array contents:"${myarray[@]}
======================================================================

and as a result I get zero elements and empty array.
(myfile.log is not empty)

What am I doing wrong?

I have found solution here

Code:
declare -a myarray

let count=0
while read tmpline
do
  myarray[$count]="$tmpline"
  let count=count+1
done < <(tail -n20 "myfile.log" && echo " ")


echo "N. of elements:"${#myarray[@]}

echo "Array contents:"${myarray[@]}
works for me

Last edited by igor.R; 04-17-2009 at 09:09 PM. Reason: fix
 
Old 04-17-2009, 09:29 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
The line: done < <(tail -n20 "myfile.log" && echo " ") does not look (to me) to be syntactically correct.
I think that done << $(tail -n20 "myfile.log" && echo " ") might work better, but Jeremy has installed a new "redface" icon for me to use if (when?) I'm wrong (again).

Last edited by PTrenholme; 04-17-2009 at 09:30 PM.
 
Old 04-17-2009, 09:38 PM   #7
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by PTrenholme View Post
The line: done < <(tail -n20 "myfile.log" && echo " ") does not look (to me) to be syntactically correct.
I think that done << $(tail -n20 "myfile.log" && echo " ") might work better, but Jeremy has installed a new "redface" icon for me to use if (when?) I'm wrong (again).
The space between the first < and second < is mandatory! Although, it should be noted that, between the two <'s, you can have as many spaces as you want. You can even use a tab between the two <'s, they just can't be directly connected.
 
Old 04-18-2009, 01:27 AM   #8
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Thanks igor.R,

That's the answer I was looking for. My reading of the man page led me to believe that the first "<" wasn't necessary. Obviously it is.
 
Old 04-18-2009, 12:05 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by igor.R View Post
The space between the first < and second < is mandatory! Although, it should be noted that, between the two <'s, you can have as many spaces as you want. You can even use a tab between the two <'s, they just can't be directly connected.
O.K., as I said:
 
  


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
Initializing arrays in bash Raht Programming 1 03-12-2009 01:33 PM
Bash Arrays Simon256 Programming 2 02-17-2009 02:39 PM
LXer: Bash Arrays LXer Syndicated Linux News 0 06-20-2008 07:20 PM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 11:49 PM
bash arrays question introuble Programming 1 05-20-2006 04:07 AM


All times are GMT -5. The time now is 01:55 PM.

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