Is "2nd level indirection" possible in bash?

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Thu Aug 30 18:25:42 UTC 2012


Solution is


    eval yyyymmdd="\${dataline:${f_yr}}\${dataline:${f_mo}}\${dataline:${f_dy}}"

Perhaps, a better approach might be something like

    yyyy=${dataline:25:4}
    mm=${dataline:32:2}
    dd=${dataline:37:4}
    yyyymmdd=$yyyy$mm$dd

You would have to keep "f_yr", "f_mo", "f_dy" somewhere, so
"yyyy", "mm", "dd" would be the same hassle.
-- 
William



----- Original Message -----
> From: Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org>
> To: Toronto Linux Users Group <tlug-lxSQFCZeNF4 at public.gmane.org>
> Cc: 
> Sent: Thursday, August 30, 2012 1:48:26 PM
> Subject: [TLUG]: Is "2nd level indirection" possible in bash?
> 
>   I'm trying to do the same bash processing on 3 text files with
> different formats.  The processing is linear.  The main loop is...
>    while read
>    do
>       dataline=${REPLY}
> # do a bunch of processing
>    done < input.txt
> 
>   Everything is the same, except the column numbers of the various items
> I'm processing.  I don't want to keep 3 copies of basically the same
> file, with column numbers changed.  Here's my "Plan A";
> 
> * set up 3 separate "format files", e.g...
> 
> f_yr="25:4"
> f_mo="32:2"
> f_dy="37:4"
> f_data="50:5"
> 
> * "source" the appropriate "format file" for the text file 
> I'm
>   processing, and use those values in the script.  Here's one function
>   as an example
> 
> #
> # 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
> }
> 
>   This does not work.  Is "2nd level indirection" allowed in bash?  My
> "Plan B" is a heavy-handed hack...
> 
> * Keep a master copy of the script with lines like...
> 
>    yyyymmdd="${dataline:f_yr}${dataline:f_mo}${dataline:f_dy}"
> 
> * Replace the "format files" with scripts that run sed, and generate a
>   temporary version to do the processing.  E.g...
> 
> #!/bin/bash
> sed "s/f_yr/25:4/
> s/f_mo/32:2/
> s/f_dy/37:4/
> s/f_data/50:5/" fluxmaster.txt > f1
> chmod 744 f1
> 
> * This changes the example script line to...
> 
>    yyyymmdd="${dataline:25:4}${dataline:32:2}${dataline:37:4}"
> 
> * Then run the temporary copy (i.e. f1).
> 
>   Any ideas/suggestions/improvements?
> 
> -- 
> 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
> 
--
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