keepthisspam
January 29th, 2010, 03:43 AM
Hey guys.. I'm new to Bash programming and I'm encountering this weird problem..
I'm trying to read a text file line-by-line and store into an array..
However, by using the WHILE-LOOP to read each line from file, the variables (normal var or arrays) doesn't seem to update..
The variables were automatically cleared upon exiting the WHILE-LOOP and revert back to it's previous state.
#!/bin/bash
clear; echo
x=10
echo "Original X: $x"
i=0
cat "test.txt" | while read FileLine
do
MyArray[$i]="$FileLine"
x=$(($x + 1))
echo "Array ($i): ${MyArray[$i]}"
i=$(($i + 1))
done
echo
echo "Array (*): ${MyArray
}"
echo "No.of Elements: ${#MyArray
}"
echo "Is X updated?: $x"
echo
Contents of "test.txt"
line1 x
line2 y
line3 zThe output in the terminal was:
Original X: 10
Array (0): line1 x
Array (1): line2 y
Array (2): line3 z
Array (*):
No.of Elements: 0
Is X updated?: 10You can see that variable X was not updated and reverted back to 10.
Are they ways or workarounds to get the variables updated..?
I'm trying to read a text file line-by-line and store into an array..
However, by using the WHILE-LOOP to read each line from file, the variables (normal var or arrays) doesn't seem to update..
The variables were automatically cleared upon exiting the WHILE-LOOP and revert back to it's previous state.
#!/bin/bash
clear; echo
x=10
echo "Original X: $x"
i=0
cat "test.txt" | while read FileLine
do
MyArray[$i]="$FileLine"
x=$(($x + 1))
echo "Array ($i): ${MyArray[$i]}"
i=$(($i + 1))
done
echo
echo "Array (*): ${MyArray
}"
echo "No.of Elements: ${#MyArray
}"
echo "Is X updated?: $x"
echo
Contents of "test.txt"
line1 x
line2 y
line3 zThe output in the terminal was:
Original X: 10
Array (0): line1 x
Array (1): line2 y
Array (2): line3 z
Array (*):
No.of Elements: 0
Is X updated?: 10You can see that variable X was not updated and reverted back to 10.
Are they ways or workarounds to get the variables updated..?