Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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[@]}
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 07:01 PM.
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[@]}
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 08:09 PM.
Reason: fix
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 08:30 PM.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.