scripting tip needed

Giorgos Keramidas keramida at ceid.upatras.gr
Wed Jul 1 19:16:35 UTC 2009


On Wed, 1 Jul 2009 18:40:00 +0100, Alexandre Vieira <nullpt at gmail.com> wrote:
> Hi folks,
>
> I'm having a little problem.
>
> For exambe in ksh:
>
> $ z=0
> $ y=1
> $ x=aaaa
> $ eval `echo MACHINE_DISK$z[$y]`=$x
> $ echo ${MACHINE_DISK0[1]}
> aaaa
>
> My problem is getting back the value of the variable using variables to
> refer to the variable name (confusing). I.e $MACHINE_DISK$z[$y] .
>
> Using ${} works great when you only have a simple array but adding a
> variable in the middle of the variable name seems to ruin it.
>
> $ echo ${MACHINE_DISK0[$y]}
> aaaa
> $ echo ${MACHINE_DISK$z[$y]}
> ksh: ${MACHINE_DISK$z[$y]}: bad substitution

My own short piece of (slightly off-topic) advice is:

  ``If you find yourself using arrays in bash, ksh or another shell,
  now is a *very* good time to consider a more advanced scripting
  language, like Perl or Python.''

Trying to coerce shell quoting to do a sensible thing with array syntax
is error-prone, annoying and --very often-- a waste of your time:

Using an interactive language like Python you can actually *test* the
code as you are writing it.  This is a major win most of the time.



More information about the freebsd-questions mailing list