Array not surviving while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array not surviving while loop
# 1  
Old 04-30-2010
Array not surviving while loop

So I'm trying to read datafile into an array, with each line representing one variable in the array. I'm successful at first but somehow it keeps getting erased.

Code:
i=0
grep '.*' datafile | while read line
do
	echo $i
	array[$i]=$(echo $line)
	echo ${array[1]} #printing array to make sure it's still there
	(( i++ ))
done

echo ${array[1]} #isn't there anymore

Sooo, do any of you know what's happening?
# 2  
Old 04-30-2010
Probably because of pipe. ( that creates sub shell )
and the value of array is not exists in the parent shell.

Code:
i=0
while read line
do
 ..
 ..
done < datafile

# 3  
Old 04-30-2010
anchal_khare is right !
but why use echo ?
Code:
array[$i]=$(echo $line)
# could be
array[$i]="$line"

better with quotes if any special character in $line.
# 4  
Old 04-30-2010
Your script is relying on undefined behavior. If you want it to work as is, use ksh instead of bash. Bash made an implementation choice that break your logic.
# 5  
Old 04-30-2010
In ksh this would work, but as others have noted one of the intricacies of bash is that the part after the pipe get run in a subshell. You do not need a grep statement and a pipe, you can use an if or case statement inside the loop to filter out empty lines.

If you still need to use a pipe into a while loop in bash you can do this:
Code:
i=0
grep '.*' datafile | {
while read line
do
        echo $i
        array[$i]=$(echo $line)
        echo ${array[1]} #printing array to make sure it's still there
        (( i++ ))
done

echo ${array[1]} #isn't there anymore
}

# 6  
Old 04-30-2010
You can try this technique for reading the file into an array :
Code:
$ cat read_file.sh

i=0
while IFS= read line
do
  set_array="${set_array}array[$i]='${line}';"
  (( i++ ))
done < inputfile
eval ${set_array}

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

$ cat inputfile
First line
Line 2
Last line
$ ./read_file.sh
array size=3
array[0]=First line
array[1]=Line 2
array[2]=Last line
$

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

Loop over array in batches

given an array of filenames files=(*) how can i loop over these in batches of four, or even better, work always with four files simultaneously. i want do something with these files, always four of them simultaneously, and if one is ready the next should start. one idea, but definitely not... (2 Replies)
Discussion started by: dietmar13
2 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

Array with do while and if loop

Hi All, I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array. So far I can read the array, but I can't get the if statement to work. Any suggestions... (5 Replies)
Discussion started by: nitin
5 Replies

5. What is on Your Mind?

Surviving in the business world - which tools to know

Hello everyone, I have been in the corporate world for a couple of years now, and I have realized that most successful/semi-productive people are those that are a hybrid between technically proficient in Visualbasic/Access while at the same time in the corporate sphere, meaning Powerpoints and... (2 Replies)
Discussion started by: awayand
2 Replies

6. Shell Programming and Scripting

Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem? Here I prepared a sample for you to see my problem; zgrw@Rain:~$ cat test asd 123... (4 Replies)
Discussion started by: zgrw
4 Replies

7. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 Replies

8. Shell Programming and Scripting

Help with awk in array in while loop

Hi everyone:) I have 2 files - IN & OUT. Example: IN A:13:30 B:45:40 . . . UNLIMITED OUT Z:12:24 Y:20:15 . . . UNLIMITED I want first row of numbers of IN - OUT. Example 13-12 45-20 My code is (2 Replies)
Discussion started by: vincyoxy
2 Replies

9. Shell Programming and Scripting

Array in loop is acting up

Hello! I have a question about loops and arrays. I'm trying to go through this: for aa in 01 02 03 OrigNum=$(grep ${Orig} Ba3In2F12.prepos | wc -l) OrigNum=$((${OrigNum} - 1)) echo ${OrigNum} etc It gets stuck on the second line. The error reads: ./asdf: line 30:... (5 Replies)
Discussion started by: RisingSun
5 Replies

10. Shell Programming and Scripting

How to use array values after the loop.

- I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop. Plz go through the script and suggest how... (1 Reply)
Discussion started by: Devesh5683
1 Replies
Login or Register to Ask a Question