James Bensley
2009-Jun-19 12:43 UTC
[CentOS] Shell Script: Simple array usage = bad substitution?
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? James ;) -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GIT/MU/U dpu s: a--> C++>$ U+> L++> B-> P+> E?> W+++>$ N K W++ O M++>$ V- PS+++ PE++ Y+ PGP t 5 X+ R- tv+ b+> DI D+++ G+ e(+++++) h--(++) r++ z++ ------END GEEK CODE BLOCK------
Bob Beers
2009-Jun-19 13:15 UTC
[CentOS] Shell Script: Simple array usage = bad substitution?
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.html>