Reading from while loop into an array


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading from while loop into an array
# 1  
Old 08-12-2008
Reading from while loop into an array

Hi
I have something like
cat $HOME/all_dirs | while read ln_old_dirs
do
if [ "$ln_all_old_dirs" != "" ]
then
echo "$ln_all_old_dirs"
fi
done

As you know that the variable ln_all_old_dirs is not accessable from outside the while loop
How to assign it in an array so that i can use it outside
It would be of great help
Thanks in advance
Suresh
# 2  
Old 08-12-2008
Code:
#!/bin/bash
array=(red green blue yellow magenta)
len=${#array[*]}
echo "The array has $len members. They are:"
i=0
while [ $i -lt $len ]; do
	echo "$i: ${array[$i]}"
	let i++
done

Take a reference from this script.

- nilesh
# 3  
Old 08-12-2008
Hi nilesh
Thanks for your reply but mine is not fixed array it can have any number of entries greater than zero
array=($ln_all_old_dirs)
len=${#array[*]}
echo "The array has $len members. They are:"
i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
done
This is not working for me
moreover my main question is how to access set of values that is array outsid the loop
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

2. Shell Programming and Scripting

Reading a file into an array

Hi I have a file with contents as below : server | ABC Issue : File System Missing XYZ Issue : Wrong Syntax PQR Issue : Old File to be removed Now I am looking for an o/p similar to server <tab> ABC Issue : File System Missing <tab> XYZ Issue : Wrong Syntax <tab>... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

3. 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

4. Shell Programming and Scripting

Reading a file into array

Hi, I need to read a file into array and print them in a loop:- 1st file :-cat a.txt RC1 RC2 RC3 RC4 My Program:- #!/bin/ksh index=0 while do read cnt<a.txt print "cnt value is ${cnt} index=`expr $index + 1` done Code tags for code, please. (5 Replies)
Discussion started by: satishmallidi
5 Replies

5. 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

6. Shell Programming and Scripting

Help with reading column in array

I have some version problem to use this code in my server while read line; do read -A array <<<$line <---------- the server dont read <<< n=${#array} for ((i=1;i<$n;i++)); do print "${array}" done func=${array} data1=${array} data2=${array} ... (1 Reply)
Discussion started by: gavin_L
1 Replies

7. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

8. Shell Programming and Scripting

Reading from array

Hi, I have an array like below... Table="table1" Table="table2" Table="table3" Table="table4" ..... Table="tablen" I want to retireve the values from the array and need to pass this to a db2 command to create a view like below. the number of values in the array will vary ... (1 Reply)
Discussion started by: ratheeshjulk
1 Replies

9. Shell Programming and Scripting

reading values into an array

I have this portion of a script and it works: base_config="A B C D E" for field in $base_config do var=`echo $field` ((i=$i+1)) done I get an array of var=A, var=B, and so on. What if I have this example with white space and want to put it into an array while... (1 Reply)
Discussion started by: djehresmann
1 Replies

10. UNIX for Dummies Questions & Answers

Reading a file into an array

I have a file that is a text file, how to get all the words into and array, i am able to get each line but not each word :(. Here is what i searched and already found...https://www.unix.com/shell-programming-scripting/99207-pipe-text-file-into-array.html. This one reads a whole line into... (6 Replies)
Discussion started by: SasankaBITS
6 Replies
Login or Register to Ask a Question