Is "2nd level indirection" possible in bash?

Walter Dnes waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org
Sat Sep 1 06:30:39 UTC 2012


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...

f_yr="25:4"
f_mo="32:2"
f_dy="37:2"
f_data="50:5"

...with the formatfile name passed as the 2nd parm on the commandline,
the bash script commands...
============================
. ${2}
data="\${dataline:${f_data}}"
yyyy="\${dataline:${f_yr}}"
mm="\${dataline:${f_mo}}"
dd="\${dataline:${f_dy}}"
dataline="2432187.20,  1248.2754,  1947,  01,  01,     .0,     .0,     .0"
echo "${dataline}"
echo "${yyyy} ${mm} ${dd} ${data}"
echo "${!yyyy} ${!mm} ${!dd} ${!data}"
eval echo "${yyyy} ${mm} ${dd} ${data}"
============================

...produce the output
============================
2432187.20,  1248.2754,  1947,  01,  01,     .0,     .0,     .0
${dataline:25:4} ${dataline:32:2} ${dataline:37:2} ${dataline:50:5}
   
1947 01 01 .0
============================

  The "!" dereferences don't output anything, but the eval works.

-- 
Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org>
I don't run "desktop environments"; I run useful applications
--
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