From the begining
if [ $line -ge $zero ]
what data type should be in $line?
-ge used in numeric comparison. If $line is a string than use = or if you just wnt to check that it's not empty use this syntax if [[ "$line" ]]
Next.
a+=($line)
Again if $line is a string then you should wrap in "" like this a+=("$line")
coz line can contain spaces.
For loop.
for i in ${a[@]}
do
echo $(a[$i]) ## here I am trying to print them
You'r messing with syntax here for i in ${a[@]} will iterate over arrays values, not indexes. And again if values are strings use "" like this for i in "${a[@]}"
So this echo $(a[$i]) won't work by 2 reasons. First you should use {} here, like this echo ${a[$i]}, second $i is not index, but it's may actualy work if it's a digit but in a wrong way. So here you need just echo $i coz $i is alredy a value from a array. Or rewrite foor loop.
for i in ${!a[@]}
do
echo ${a[$i]} ## here I am trying to print them
done
And last but not least, there is no done at the end of this script or it's just a part? So in the and it should look like this.
while read line; do
if [[ $line ]]; then
a+=( "$line" ) ##here I am attempting to store the values in an array
else
for i in ${!a[@]}; do
echo ${a[$i]} ## here I am trying to print them
done
fi
done < data.txt
echo ${a[@]} # print rusult
$(a[$i])is a command substituion, What are you really trying to do? cfor i in ${a[@]}will iterate over the values in the array, not its indexes; that is, if the array isa=(100 507 12), theniwill be set to 100, then 507, then 12. So you wantecho "$i"instead ofecho ${a[$i]}(let aloneecho ${a[$i]}which, as @Jetchisel pointed out, does something completely different).