Array; while read line do problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array; while read line do problem
# 1  
Old 01-05-2011
Array; while read line do problem

Hi - I don't understand why the following script isn't working. I want to read the contents of a while loop into an array, but it looks like the array is destroyed once the while loop is finished. Can anybody help?

someFile:
Code:
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

Script:
Code:
cat someFile |
        while read l; do
                echo "array[${#array[*]}]=\"$l\""
                array[${#array[*]}]="$l"
        done

for ((i=0;i<${#array[*]};i++)); do
        echo ${array[i]}
done

Output:
Code:
array[0]="PC_1 wf_test1 Test"
array[1]="PC_2 wf_test2 Test"
array[2]="PC_3 wf_test3 Test"

Desired Output:
Code:
array[0]="PC_1 wf_test1 Test"
array[1]="PC_2 wf_test2 Test"
array[2]="PC_3 wf_test3 Test"
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

# 2  
Old 01-05-2011
I'm not at a machine to run your code...but, where within the while loop are you setting and incrementing the array subscript?

Code:
COUNT=1
cat someFile | while read l; do
       echo "array[$COUNT]=\"$l\""
       array[${COUNT}]="$l"
       ((COUNT += 1))
done

# 3  
Old 01-05-2011
Your problem is the piped while read starts a sub shell:

Have a look at this wiki page on the issue.

Code:
#!/bin/bash
while read l; do
    echo "array[${#array[*]}]=\"$l\""
    array[${#array[*]}]="$l"
done < someFile
for ((i=0;i<${#array[*]};i++)); do
        echo ${array[i]}
done


Last edited by Chubler_XL; 01-05-2011 at 11:21 PM..
# 4  
Old 01-05-2011
@Chubler XL - u r right on. Thanks for that link -- very handy. I've had problems w/ while loops before and never knew what the deal was.

@purdym - no need for the extra increment since arrays are zero-based index.
array[${#array[*]}]="$l"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Read line by line and store a word in array

Hi, I would like to read a file line by line and then store the whole line word by word in array so that I can do specific manipulation on each word. $ cat test.txt hello how are you i am good I would like to have an array created dynamically so that first time , the... (4 Replies)
Discussion started by: ashwin3086
4 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Problem with read line

Hi everybody, I am new to shell script. Please help me with this problem. I have a file test.txt with the content like this: I have a shell script test.sh like this #!/bin/sh while read line do echo $line >> out.txt done < test.txt out.txt is expected to have the same content... (10 Replies)
Discussion started by: Dark2Bright
10 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

problem to read data in array

My input is a long list of data start with "#": #read_1 123456898787987 #read_2 54645646540646406 #read_3 4654564654316 . . I got a bit confusing about how to set all in an array first. And then when I run a program name "statistic_program", it will read the array in scalar and do... (24 Replies)
Discussion started by: patrick87
24 Replies

10. Shell Programming and Scripting

I need suggestion on problem read a file line by line and do stuff

At first, give my best wish for all MOD and admins here. I"m learning bash shell program just for a month, not too much, not too little, but i must admire that i'm very bad at math and algorithm. :( I want to do this : Read the content of a file line by line and at each line, ask me want to... (3 Replies)
Discussion started by: madi3d8
3 Replies
Login or Register to Ask a Question