[Novalug] bash question

Jon LaBadie novalugml@jgcomp.com
Mon Aug 11 19:59:03 EDT 2014


On Mon, Aug 11, 2014 at 06:02:31PM -0400, Peter Larsen via Novalug wrote:
> On 08/11/2014 05:18 PM, Pete Nuwayser via Novalug wrote:
> > looking for some community WD-40 to get the rust out :)
> >
> > I'm pulling user-data from an AWS instance like this:
> > read var1 var2 var3 <<< $(curl --silent http://169.254.169.254/latest/user-data)
> >
> > where the data is already set as three space-separated parameters.
> >
> > I now want to convert the contents of these three variables to
> > lowercase. I could do it like this:
> > var1="$(tr "[A-Z]" "[a-z]" <<< "$var1")"
> > var2="$(tr "[A-Z]" "[a-z]" <<< "$var2")"
> > var3="$(tr "[A-Z]" "[a-z]" <<< "$var3")"
> >
> > but I'd rather use some kind of for loop. several experiments and
> > searches for how to do the proper substitution have failed me. any
> > thoughts? I'm not married to using tr - it just needs to be able to
> > run in a bash script. TIA
> 
> Why use a for loop? Why not put the tr upper to lower in your curl line
> when you retrieve the data?
> 

Agree with Peter, change case before the assignment.  But if
you can't, assign to array elements rather than descrete vars.

  read var[1] var[2] var[3] <<< $(...

then your loop could be:

for i in 1 2 3    # or    for ((i=1; i<=3; i++))
do
    var[$i]=$(tr [:upper:] [:lower:] <<< "${var[$i]}")
done


I tried using the descrete vars but didn't find the magic
combination of "eval" and quoting.

Jon
-- 
Jon H. LaBadie                  novalugml@jgcomp.com
 11226 South Shore Rd		(703) 787-0688 (H)
 Reston, VA  20190		(609) 477-8330 (C)



More information about the Novalug mailing list