List of lists in Bourne shell

List of lists in Bourne shell

Post by Will Cardwel » Wed, 05 Jun 2002 18:57:58



How can I do this kind of thing in the Bourne shell? Below, I want to have a
list of sites, each with a list of 4 parameters. Then I want to have an
outer loop of sites with inner loop of parmeters per site.
Thanks so much for any help or advice.
Will Cardwell

#!/bin/sh
# This doesn't work correctly.
#  It would be nice to eliminate this separate "site=..." list too...
# site list
site="acme baker charles"
# 4 parameters for each site
acme="ac01 ac02 ac04 ac04"
baker="bk01 bk02 bk03 bk04"
charles="ch01 ch02 ch03 ch04"
#
for s in $site
do
  echo "$s"
  for parm in "$s"
  do
    echo " $parm"
  done
  echo "---end of site parms---"
done

 
 
 

List of lists in Bourne shell

Post by Jonas Melli » Wed, 05 Jun 2002 20:04:46



> How can I do this kind of thing in the Bourne shell? Below, I want to have a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.
> Will Cardwell

> #!/bin/sh
> # This doesn't work correctly.
> #  It would be nice to eliminate this separate "site=..." list too...

In Bourne shell? Use another script language. For example, Perl, Tcl,
awk, etc.

Quote:> # site list
> site="acme baker charles"
> # 4 parameters for each site
> acme="ac01 ac02 ac04 ac04"
> baker="bk01 bk02 bk03 bk04"
> charles="ch01 ch02 ch03 ch04"
> #
> for s in $site
> do
>   echo "$s"

try
   ps="echo $"$s
   for parm in `eval $ps`
instead of

Quote:>   for parm in "$s"
>   do
>     echo " $parm"
>   done
>   echo "---end of site parms---"
> done

Basic technique, construct a string containing the command, then
evaluate this string as a command.

 
 
 

List of lists in Bourne shell

Post by Thomas Weidenfell » Wed, 05 Jun 2002 20:36:19



> How can I do this kind of thing in the Bourne shell? Below, I want to have a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.

It is not clear for me what you really want, so I just give you a few
variations (just typed, might have syntax errors):

        data="acme ac01 ac02 ac04 ac04
        baker           bk01 bk02 bk03 bk04
        charles         ch01 ch02 ch03 ch04"

        echo "$data" | while read s p1 p2 p3 p4 ; do
                echo $s
                echo $p1
                echo $p2
                echo $p3
                echo $p4
        done

#######

        site="acme baker charles"
        acmeParam="ac01 ac02 ac04 ac04"
        bakerParam="bk01 bk02 bk03 bk04"
        charlesParam="ch01 ch02 ch03 ch04"

        for s in $site ; do
                echo $s
                eval param=\"\$${s}Param\"
                for p in $param ; do
                        echo $p
                done
        done

#######

        data="acme ac01 ac02 ac04 ac04 -- baker bk01 bk02 bk03 bk04 -- charles ch01 ch02 ch03 ch04 --"

        set -- $data
        while [ $# -gt 0 ] ; do
                s=$1
                shift
                while [ $1 != "--"  ] ; do
                        print $1
                        shift
                done
                shift
        done

/Thomas

 
 
 

List of lists in Bourne shell

Post by Carl Lindgre » Thu, 06 Jun 2002 00:41:42



Quote:> How can I do this kind of thing in the Bourne shell? Below, I want to have
a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.
> Will Cardwell

Creat a text file containing the data (you will use this to direct the data
into the script - for an example:

acme ac01 ac02 ac04 ac04
charles ch01 ch02 ch03 ch04
baker bk01 bk02 bk03 bk04
charles ch01 ch02 ch03 ch04
EOF
------------------------------------------------------
Then invoke the script by:

Quote:>>$sh script_name.sh < data_file_name.txt

-------------------------------------------------------
#!/bin/sh
while read $list p1 p2 p3 p4 p5
 do
  echo $p1
  echo $p2
  echo $p3
  echo $p4
  echo $p5
  echo $p1 $p2 $p3 $p4 $p5
 done

--
Carl Lindgren
C. R. Lindgren Consulting
Minneapolis, MN

 
 
 

List of lists in Bourne shell

Post by th » Thu, 06 Jun 2002 01:18:56



> How can I do this kind of thing in the Bourne shell? Below, I want to have a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.
> Will Cardwell

Hi,
try the following modification.

Tankred

Quote:> #!/bin/sh
> # This doesn't work correctly.
> #  It would be nice to eliminate this separate "site=..." list too...
> # site list
> site="acme baker charles"
> # 4 parameters for each site
> acme="ac01 ac02 ac04 ac04"
> baker="bk01 bk02 bk03 bk04"
> charles="ch01 ch02 ch03 ch04"
> #
> for s in $site
> do
>   echo "$s"
    eval "t=\$$s"
>   #for parm in "$s"
    for parm in $t
>   do
>     echo " $parm"
>   done
>   echo "---end of site parms---"
> done

 
 
 

List of lists in Bourne shell

Post by Willia » Thu, 06 Jun 2002 01:45:12



Quote:> How can I do this kind of thing in the Bourne shell? Below, I want to have
a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.
> Will Cardwell

> #!/bin/sh
> # This doesn't work correctly.
> #  It would be nice to eliminate this separate "site=..." list too...
> # site list
> site="acme baker charles"
> # 4 parameters for each site
> acme="ac01 ac02 ac04 ac04"
> baker="bk01 bk02 bk03 bk04"
> charles="ch01 ch02 ch03 ch04"
> #
> for s in $site
> do
>   echo "$s"
>   for parm in "$s"
>   do
>     echo " $parm"
>   done
>   echo "---end of site parms---"
> done

How about something like this (in portable bourne shell,
ksh-ers would have some optimizations, and the loop index
should be handled mathmatically - using expr if nothing
else):

site0="acme ac01 ac02 ac04 ac04"
site1="baker bk01 bk02 bk03 bk04"
site2="charles ch01 ch02 ch03 ch04"

for Ix in 0 1 2 3 4 5 6 7 8 9 10
do
   eval siteParam=\"\${site}$Ix\"
   if [ -n "$siteParam" ] ; then
      set -- $siteParam
      echo "$1"
      while [ $# -gt 1 ] ; do
         shift
         echo " $1"
      done
   else
      break;
   fi
done

Obviously, that's limited to 11 sites, but you get
the idea. -Wm

 
 
 

List of lists in Bourne shell

Post by Will Cardwel » Thu, 06 Jun 2002 09:44:44


Thanks so much to all. Some great and amazing approaches here, from very
simple to fuller features. I'll probably use a simple eval method now, but
may go to variable-length-check-for empty-string-to-terminate method later.

Will

 
 
 

List of lists in Bourne shell

Post by Robert Towste » Thu, 06 Jun 2002 20:29:20


Here ya go =)

#!/bin/sh
site="acme:ac01;ac02;ac04;ac04 baker:bk01;bk02;bk03;bk04
charles:ch01;ch02;ch03;ch04"
for s in $site
do
   sname=`echo $s | cut -d ":" -f 1`
   sfield=`echo $s | cut -d ":" -f 2`
   echo "Site name is $sname"

   OLDIFS=$IFS
   IFS=";"
   for parm in $sfield
   do
     echo "        $parm"
   done
   IFS=$OLDIFS
   echo "---end of site parms---"
done


> How can I do this kind of thing in the Bourne shell? Below, I want to have a
> list of sites, each with a list of 4 parameters. Then I want to have an
> outer loop of sites with inner loop of parmeters per site.
> Thanks so much for any help or advice.
> Will Cardwell

> #!/bin/sh
> # This doesn't work correctly.
> #  It would be nice to eliminate this separate "site=..." list too...
> # site list
> site="acme baker charles"
> # 4 parameters for each site
> acme="ac01 ac02 ac04 ac04"
> baker="bk01 bk02 bk03 bk04"
> charles="ch01 ch02 ch03 ch04"
> #
> for s in $site
> do
>   echo "$s"
>   for parm in "$s"
>   do
>     echo " $parm"
>   done
>   echo "---end of site parms---"
> done

 
 
 

1. bash shell scripting: Can I change how "for x in list" parses list?

I am writing a script to automate LAME and I'm dealing with a
directory of filenames that are full of whitespace. I am trying to
parse out the song titles in a loop:

for SONG in `ls *.wav | sed -e 's/*.wav$//'
do
        ** parse $SONG to get ID3 tag info **
        ** encode $SONG.wav to $SONG.mp3 **
        rm -f $SONG.wav
done

The problem is that the for statement is considering whitespace in the
filenames to be valid delimitations, so it's setting SONG to be each
individual word in the filenames instead of the complete song titles:

$SONG=Yasunori
$SONG=Mitsuda
$SONG=-
$SONG=Chrono
$SONG=Cross
$SONG=OST
$SONG=Disc
$SONG=1
$SONG=track
$SONG=1
$SONG=-
$SONG=Scars
$SONG=of
$SONG=Time

If I simply do the `ls *.wav | sed -e 's/*.wav$//' command, I get
proper output on the console. So, can I get the for statement to
tokenize on newlines only?
I could do several artificial solutions, like replacing the whitespace
with underscores or adding special delimiters and catting the $SONGs
together, but I would love to know if there's a good elegant way to do
this. I'm sure it's been dealt with before, but I've had no luck
searching for the answer.

2. Best way to upgrade XFree86?

3. mailing list for bourne shell

4. Cool

5. List of plug-ins for linux/netscape please!

6. tcsh logs off terminal after 20 min

7. Light Speed Bourne Shell! (was: Bourne shell tricks)

8. Mouse and lilo problems

9. alt.uu.comp.os.linux.questions list.linux-activists.kernel list.linux-activists.serial list.linux-activists.problem-reports list.linux-activists.question

10. 4.2BSD C Shell Bug List available -- many apply to current C Shells

11. MAILING-LIST: Taper list is up

12. Linux Incompatibility List - main list 11-06-97

13. MAILING-LIST: Linuxisp Mailing list