nested loops

Saumen K Dutta skdutta at cs.tamu.edu
Fri Aug 10 16:23:53 AEST 1990


In <4103 at herbie.misemi> article adley at herbie.misemi writes:   
->
->Hi,
->We are trying to write a Bourne shell script that includes two 
->nested loops as follows
->
->!# /bin/sh
->this="one two three"
->that="four five six seven"
->them="eight nine ten"
->all="this that them"
->#
->for i in $all
->do
->for j in $`echo $i`
->do
->echo $i $j
->done
->done
->
->
->we want (and expected!) the output:
->this one
->this two
->this three
->that four
->that five
->that six
->that seven
->them eight
->them nine
->them ten
->
->The problem lies in the  $`echo $i` statment
->the `echo $i` preforms as expected and the result is one of this,
->that, or them
->the $ infront of this statment however is taken as a literal $
->and does not return the value of the variable this or that or them
->instead it returns $this or $that or $them.
->

If you modify the code in the following way it will work. I bet there
must be a simpler way of doing this:

#! /bin/sh

this="one two three"
that="four five six seven"
them="eight nine ten"
all="this that them"
#
for i in $all
do
k='eval echo $`echo $i`'
for j in `eval $k`
do
echo $i $j 
done
done


--
     _                                   ||Internet: skdutta at cssun.tamu.edu  
    (   /_     _ /   --/-/- _            ||Bitnet : skd8107 at tamvenus.bitnet 
   __)_/(_____(_/_(_/_(_(__(_/_______    ||Uucp : uunet!cssun.tamu.edu!skdutta
                                 ..      ||Yellnet: (409) 846-8803



More information about the Comp.unix.wizards mailing list