[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug in cron postinst



Matt Zimmerman wrote:
> On Wed, Sep 03, 2003 at 05:20:54PM +0100, Colin Watson wrote:
> 
> > > mrvn@dual:/tmp/bar% bash
> > > bash-2.05b$ L=`find`              
> > > bash-2.05b$ for i in $L; do echo $i; done
> > > .
> > > ./a
> > > b
> > 
> > No wonder. You aren't quoting correctly! Use 'echo "$i"'.
> 
> there:[~] /bin/bash
> mdz@there:~$ foo="a b"
> mdz@there:~$ for x in $foo; do echo "$x"; done
                        ^^^^
> a
> b

$ foo="a  b"; for x in "$foo"; do echo "$x" : $x; done
a  b : a b

note that whenever you use "$variable" you need to enclose it in quotes
to protect it from word separation. zsh users are excused from this
excercise.

looking back at the original script, we see the problem began with
find(1) which has a very hard time with whitespace (newlines,
specifically) in filenames. enclosing the invocation in backticks
guarantees that the $L variable will be broken if a filename has any
whitespace (spaces, tabs, or newlines) in it.

the -print0 command to GNU find gets around this problem nicely, as the
NULL character is not a valid filename character to begin with. it is
left as an excercise for the reader to properly parse the output of
-print0 for their own specific application.

-john



Reply to: