Another bash scripting problem...

Colin Watson cjwatson at ubuntu.com
Wed Jun 22 14:59:36 UTC 2005


On Wed, Jun 22, 2005 at 03:51:33PM +0200, Markus Kolb wrote:
> ZIYAD A. M. AL-BATLY wrote on Tue, Jun 21, 2005 at 15:34:44 +0300:
> > On Mon, 2005-06-20 at 22:04 -0400, Matthew S-H wrote:
> > > It is supposed to source all of the files in a directory.
> > > However, it needs to make sure of a few things.  It needs to own
> > > ~/.profile_sections, and it shouldn't recurse into sub-directories.
> > 
> >   find ~/.profile_sections -maxdepth 1 -type f
> > or if you want sylinks to be sourced as well:
> >   find ~/.profile_sections -maxdepth 1 ! -type d
> > 
> > Above will find all the files in a directory.
> 
> Which has the overhead of new shell creation and additional program
> execution.

And using [ doesn't? (It depends on your shell; [ is sometimes a shell
builtin, but is not required to be.)

If [ is not a builtin, then the original code snippet forks many
subprocesses, whereas this forks only one.

> I don't know why he should use any external program if it can
> be done in script as well.

I'd expect 'find' to be more efficient than the original construction in
the general case. If [ happens to be a builtin, there probably isn't
much in it (you'd have to time it to know for sure), but I'd often pick
'find' because it's easier to use that correctly in the presence of
strange filenames.

In any case, the efficiency gains here are going to be small enough that
it will be premature to optimise for efficiency; optimise for
correctness and readability instead.

Cheers,

-- 
Colin Watson                                       [cjwatson at ubuntu.com]




More information about the ubuntu-users mailing list