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 and as a result I get zero elements and empty array. (myfile.log is not empty) What am I doing wrong? :scratch: |
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 |
Or you could use:
Code:
for tmpline in $(tail -n20 "myfile.log") |
@Robhogg
Wouldn't spaces in the contents of myfile.log cause problems? I.e. each "word" would be separate instance of $tmpline? |
Quote:
I have found solution here Code:
declare -a myarray |
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). |
Quote:
|
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. |
Quote:
|
All times are GMT -5. The time now is 07:37 AM. |