[CentOS] Shell Script: Simple array usage = bad substitution?

Fri Jun 19 13:15:32 UTC 2009
Bob Beers <bob.beers at gmail.com>

On Fri, Jun 19, 2009 at 8:43 AM, James Bensley <jwbensley at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.centos.org/pipermail/centos/attachments/20090619/c6568e3f/attachment-0004.html>