Subject: Re: Bourne shell question
To: David Laight <david@l8s.co.uk>
From: Daniel Eggert <danieleggert@mac.com>
List: netbsd-help
Date: 12/19/2002 17:41:45
On torsdag, dec 19, 2002, at 17:18 Europe/Copenhagen, David Laight 
wrote:

> On Thu, Dec 19, 2002 at 05:00:00PM +0100, Daniel Eggert wrote:
>> I hope this is not completely OT:
>>
>> In my shell script I have two variables. Something like this:
>> 	partitions="ld0a ld0b ld1a"
>> 	paths="/a /b /c"
>> Now I want to loop through these and access
>> 	'ld0a' with '/a'
>> 	'ld0b' with '/b'
>> 	'ld1a' with '/c'
>> Is there any easy / clever way to do this in /bin/sh?
>
> how about (untested):
>
> index() {
>     local i = $1
>     shift
>     eval echo $\$i
> }
>
> i=1;
> while
> 	p=`index $i $partitions`
> 	q=`index $i $paths`
> 	[ -n "$p" -a -n "$q ]
> do
> 	echo $p:$q
> done
>

Thanks. You allmost got it. With a bit of debugging I got this:

#!/bin/sh
index() {
     local i=$1
     shift $i
     eval echo $i $@ > /dev/null
     echo $1
}
partitions="ld0a ld0b ld1a"
paths="/a /b /c"
i=1;
while
         p=`index $i $partitions`
         q=`index $i $paths`
         [ -n "$p" -a -n "$q" ]
do
         echo $p "and" $q
         i=$(($i+1))
done


and it works like a charm!

/Daniel