Is "2nd level indirection" possible in bash?

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Sat Sep 1 07:17:28 UTC 2012


On Sat, 1 Sep 2012, Walter Dnes wrote:

> On Thu, Aug 30, 2012 at 04:33:51PM -0400, Chris F.A. Johnson wrote
>> On Thu, 30 Aug 2012, Ben Walton wrote:
>>
>>> Hi Walter,
>>>
>>>> # Routine to assemble date in YYYYMMDD format, using column locations
>>>> # imported in format file (i.e. 2nd parameter on commandline)
>>>> calc_yyyymmdd() {
>>>>    yyyymmdd="${dataline:${f_yr}}${dataline:${f_mo}}${dataline:${f_dy}}"
>>>>    export yyyymmdd
>>>> }
>>>
>>> I think this is what you're after (assuming I understand what you're
>>> trying to do):
>>>
>>> ME=ben
>>> varname=ME
>>> echo ${!varname}
>>>
>>> You build up the variable you're after then then use the ! to
>>> dereference the name.
>>
>>     In shells that don't support the indirect expansion, use eval:
>>
>> eval echo "\$$varname"
>
>  It looks like I'll have to go with eval.  bash may support variables
> with "!" dereferencing, but it doesn't seem to work with substring
> notation.  E.g. for format file...
...
> yyyy="\${dataline:${f_yr}}"

> echo "${!yyyy} ${!mm} ${!dd} ${!data}"

    Of course it won't work; the value of $yyyy is not a valid variable name.

-- 
    Chris F.A. Johnson, <http://cfajohnson.com/>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list