[wellylug] arrays in bash?
Grant McLean
grant at mclean.net.nz
Tue May 22 14:49:05 NZST 2007
On Tue, 2007-05-22 at 13:27 +1200, Sigurd Magnusson wrote:
> SilverStripe
> how do iterate of an array in bash?
Whilst technically I'm sure it's possible, why not just write the script
in a more appropriate language. I have a rule of thumb that if my
script is longer than 5 lines I rewrite it as Perl. The frustrations of
trying to achieve anything non-trivial in bash will inevitably drive me
to it anyway.
Like any rule-of-thumb, I break that rule occasionally (eg: for init.d
scripts that are essentially just a big case statement). However I
often end up with 20-30 lines of convoluted bash code and kick myself
for not switching sooner.
> e.g. can someone change this psuedocode into bash... :)
>
> myarray = ("fish", "yuck"),
> ("chocolate", "yum"),
> ("icecream", "yum")
That's not an array, that's a hash. Only a PHP code could think that
was an array :-)
> for food in $myarray; do
> echo I think that $food is $myarray[$food]
> done
If you're familiar with PHP, then why not write the script in PHP and
change the first line to:
#!/usr/bin/php
Cheers
Grant
More information about the wellylug
mailing list