OT: Bash arrays & indirection?

Ian Malone ibmalone at gmail.com
Thu Feb 16 16:48:30 UTC 2012


On 16 February 2012 15:41, Patrick Lists <fedora-list at puzzled.xs4all.nl> wrote:
> On 16-02-12 12:13, Ian Malone wrote:
>>
>> On 16 February 2012 08:43, Patrick Lists<fedora-list at puzzled.xs4all.nl>
>>  wrote:
>>>
>>> Hi,
>>>
>>> I am trying to do the following in bash:
>>>
>>> OPTIONS = ( alfa beta gamma )
>>> SUBOPTIONS_alfa = ( alfa0 alfa1 )
>>> count=0
>>> SETCOUNT = ${#SUBOPTIONS_${OPTIONS[$count]}[@]}
>>>

>> nameSUBOPTS=SUBOPTIONS_${OPTIONS[$count]}[@]
>> tmpARR=(${!nameSUBOPTS}) #You need tmpARR, because the '#' expansion
>> wont work with '!'
>> SETCOUNT=${#tmpARR[@]}
>> echo ${OPTIONS[$count]}:$SETCOUNT
>> done
>
>
> Thanks! I will try it out. It looks more sane that what I found myself which
> apparently is challenging the bash gods to unleash their wrath if I use it:
>
> eval SETCOUNT=\${#SUBOPTIONS_${OPTIONS[$count]}[@]}
>

Yes, I'd meant to mention you could always use eval, but the quoting
introduced by each additional layer of indirection will eventually
cause brain damage.

>> Finally, it's spelt 'alpha' ;)
>
>
> Heh spelt or spelled? :)

Universal law of pedantry in action...

-- 
imalone


More information about the users mailing list