Re: du

[prev] [thread] [next] [lurker] [Date index for 2005/10/01]

From: Smylers
Subject: Re: du
Date: 00:30 on 01 Oct 2005
Aaron Crane writes:

> David King writes:
> 
> > >     shopt -s nullglob
> > > At least in a vaguely contemporary bash.
> > 
> > This, of course, results in commands responding as if you typed nothing
> > where a wildcard doesn't match. So 
> > 	ls -l *.o
> > results in a directory listing instead of a slightly more helpful
> > 	ls: *.o: No such file or directory
> > 
> > Or
> > 	cat *.c|grep func_name
> > waiting on stdin for you. Which is probably worse than the wildcard crap
> > lying around.
> 
> Yes.  nullglob is useful in for loops in scripts, but I think harmful
> everywhere else.

Even more recently 'Bash' has acquired failglob -- if a glob fails to
match anything then the command line is aborted with an error stating as
much and no commands are run.  This fixes David's concerns above, and
you get the error messages from the shell rather than ls or cat or
whatever.

Actually, failglob is even better than the 'Bash' default of passing the
glob to the external program if you consider the pathological case of a
glob that doesn't match itself but where there is a file with that glob
as its name in the current directory.

For example, suppose this directory contents:

  $ mkdir new
  $ cd new
  $ touch aa bb '[ab]*' '[ef]*'

Then this glob obviously displays the matching files (the fact that
there is also a file of that name doesn't come into play):

  $ du [ab]*
  0       aa
  0       bb

This glob doesn't match anything so is passed straight through to du,
which can't find a file of that name so throws an error:

  $ du [cd]*
  du: `[cd]*': No such file or directory

This very similar glob also doesn't match anything so is passed through
to du, but there happens to be a file of that name so du acts on that
file instead of throwing an error, which isn't good:

  $ du [ef]*
  0       [ef]*

With nullglob the shell passes nothing at all to du, so du acts as it
always does when run with no arguments, which, as David pointed out
above, is confusing:

  $ shopt -s nullglob
  $ du [ef]*
  4       .

But with failglob you do get an error message:

  $ shopt -s failglob
  $ du [ef]*
  -bash: no match: [ef]*

Smylers
-- 
May God bless us with enough foolishness to believe that we can make a
difference in this world, so that we can do what others claim cannot be done.

There's stuff above here

Generated at 16:00 on 04 Oct 2005 by mariachi 0.52