Subject: Re: Iterating through "fields" in a list
To: Jan Danielsson <jan.m.danielsson@gmail.com>
From: matthew sporleder <msporleder@gmail.com>
List: netbsd-users
Date: 02/01/2007 03:30:46
On 2/1/07, Jan Danielsson <jan.m.danielsson@gmail.com> wrote:
> Hello all,
>
>    (Using /bin/sh)
>
>    I have a set of backup scripts which contain this:
>
>    tar .... | bzip2 | gpg -r $ENCTO > blarg.tar.bz2.gpg
>
>    ENCTO is a variable which is set prior to running the scrips. Now I
> have realized that I need to encrypt to several recipients. I.e.:
>
>    tar .... | bzip2 | gpg -r foo -r bar > blarg.tar.bz2.gpg
>
>    Is there some painfully obvious way to construct the string:
>
>    "-r foo -r bar"
>
>    from the environment variable:
>
>    ENCTO=foo bar
>
>    (Using space as separator)
>
>    "for" obviously uses file names, and doesn't seem to be able to use
> space separated lists for processing.
>
>    (Don't even suggest that I should create two files named "foo" and
> "bar" somewhere so that I can use "for". Even I realize how ugly *that*
> is. ;-)


Easy but slow:
FOO="foo bar"
echo $FOO | awk '{ print "-r " $1 " -r " $2 }'
-r foo -r bar

Possibly faster:
for x in `echo $FOO`
do echo -n "-r $x "
done

And, obviously, you can do whatever you need to do with the output.