Re: OT: shell scripts and spaces in file/folder names
on Thu, Apr 17, 2003 at 11:45:39PM +0200, Jan Trippler (Jan.Trippler@t-online.de) wrote:
> On Don, 17 Apr 2003 at 21:58 (+0100), Karsten M. Self wrote:
> > on Thu, Apr 17, 2003 at 01:03:28PM -0700, Alvin Oga (aoga@Maggie.Linux-Consulting.com) wrote:
> > >
> > >
> > > On Thu, 17 Apr 2003, Joris Huizer wrote:
> > >
> > > > for i in `ls .`
> > > > do
> > > > #...
> > > > done
> > >
> > > try ... (untested )
> > > #
> > > # note star instead of dot ...important...
> > > #
> > > # dont forget to cd /someplace/first
> > > # and make sure cd worked
> > > #
> > > for i in ` ls * `
> > > do
> > > ls -la ' $i '
> > > done
> > >
> >
> > Nope.
> >
> > - Environment variables don't resolve in single quotes. "$i" will
> > work.
> >
> > - You're now referring to the file " foo bar ", not "foo bar".
> > Quoting includes whitespace, including whitespace around the quoted
> > string. "$i" will give you "foo bar".
> >
> > Note that you have to re-quote if you're re-evaluating the command.
> > Which is among the reasons spaces are so nasty. Particularly, say, in
> > URLs (quoting errors are a large class of Web errors and security
> > holes).
>
> ... and try
>
> for i in *
>
> instead of
>
> for i in `ls *`
>
> You don't need to start a ls command to expand the * - the bash
> itself can do it.
There's a subtle distinction, and one that points to another error in
Alvin's script.
"for i in *" resolves to a list of files in the current directory.
"for i in `ls *`" resolves to a listing of all files, and directories,
in the current directory. That is, if you have a directory in the
current directory, the '*' expands to include the directory name, and
the 'ls' lists the _contents_ of the directory. You don't see the
directory itself. A script based on this expansion would likely break.
There are other problems which perlmongers will be familiar with.
Depending on how hostile the current environment is, files might begin
with '--' (evaluating to an 'ls' option), or other characters, possibly
executing commands on your behalf. I generally check wildcard
expansions (echo *) before relying on them.
Peace.
--
Karsten M. Self <kmself@ix.netcom.com> http://kmself.home.netcom.com/
What Part of "Gestalt" don't you understand?
"Nothing at all," intoned Marvin dismally, "not an electronic sausage."
-- HHGTG
Reply to: