On Fri, 9 Dec 2005 03:50, Matthew Hannigan wrote:
> On Thu, Dec 08, 2005 at 03:27:52PM +1100, James Gray wrote:
> > On Thursday 08 December 2005 15:15, Matthew Hannigan wrote:
> > > On Thu, Dec 08, 2005 at 12:46:16PM +1100, James Gray wrote:
> > > > Hi All,
> > > >
> > > > I've been fiddling with a script and can't quite get it to work. 
> > > > The problem is premature wild-card expansion.
> > > >
> > > > FILES="*.upd dat*.zip *.tar sdat*.exe"
> > > >  [ ... ]
> > > >     for PATTERN in $FILES
> > >
> > > Quotes around $FILES (i.e. "$FILES") should work,
> > > but you said you tried that?
> >
> > Yeh, then find gets passed something that results in:
> > find . -name "*.upd dat*.zip *.tar sdat*.exe"....  which doesn't have
> > the desired result.
>
> Uh yeah. Duh me.
>
> > John solved it for me by preventing bash from expanding filename
> > globbing by invoking the shell with "-f".  Simple and it works :)
>
> Rather than invoke with -f, you can surround the find with
> set -f and set +f to turn noglob on and off.

(Sorry for the delayed response - I've been offline and water-skiing all 
weekend!)

True - except the expansion is being done in the for loop:

for FOO in $BAR
do
    find /path -name "$FOO" ....
done

$FOO will contain a list of files matched in whatever the CWD is, so the 
entire "for" loop who need to be "set -f".  The only command that actually 
does any file matching is "find" so it's ok to run the whole script with 
"/bin/bash -f" on the hash-bang line.

It stumped me, because I didn't think bash expanded wild-cards in for loops 
when the wild-cards are contained in a shell variable.  Looking at now, it 
makes perfect sense, but it's always the unexpected things that cause you 
the most angst :)  One thing they taught me when I did my pilot's license 
was that if things just don't add up, question all your assumptions.  The 
same holds true in computing too I guess.

Cheers,

James
-- 
There are two ways of constructing a software design.  One way is to make
it so simple that there are obviously no deficiencies and the other is to
make it so complicated that there are no obvious deficiencies.
                -- C.A.R. Hoare

Attachment: pgpxS0DtQDThW.pgp
Description: PGP signature

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to