Re: Shell Script: Simple array usage = bad substitution?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Fri, Jun 19, 2009 at 8:43 AM, James Bensley <jwbensley@xxxxxxxxx> wrote:
Hey Guys n Gals;

I have some arrays that I can't seem to expand correctly (if that's
the correct word?), imagine the following example:

#!/bin/bash
myArray=("First" "Second" "Third")

First=("Monday" "Tuesdays" "Wednesday")
Second=("One" "Two" "Three")
Third=("A" "B" "C")

for ((i=0;i<${#myArray[@]};i++))
do
       for ((k=0;k<${#${myArray[$i]}[@]};k++)) # < Things go bad here!
       do
               echo "${${myArray[$i]}[$k]}" # I understand this line
is won't work but it doesn't matter right now
       done
done

So ultimatly we shall loop round each value in the myArray array and
print out the values in each array which has the same name as the
value in the myArray, array.

Where I have marked with a comment, the script stops with the error-

: bad substitution

I have looked on-line but I don't quite understand how I can correct
this? I have seen other people have this error for other problems but
not one like mine so I don't fully understand it? Can anyone explain
to me exactly what is going wrong and how I might correct it?

How about this? Avoids the c=like for loop increments and just uses
 the array elements for the next loop ...

#!/bin/bash
myArray=("First" "Second" "Third")

First=("Monday" "Tuesdays" "Wednesday")
Second=("One" "Two" "Three")
Third=("A" "B" "C")

for i in ${myArray[@]};  do
    for k in ${i};  do
        eval TMP=\${$k[@]}
        echo ${TMP}
    done
done

-Bob
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux