[Cialug] [OT]: Shell script globbing

Chris Freeman cwfreeman at gmail.com
Thu Dec 20 16:49:14 CST 2007


I might prefer 'find' instead of 'ls' if there's a limit with 'ls'. There's
also a glob limit in most shells, which may be a consideration here. (I.e.,
if you have more than 1000(??) *.bad files, the shell will die before even
calling 'ls'.)

Using 'find' might turn it into:

if [ 'find . -name "*.bad" -maxdepth 1 | wc -l` ]

The 'maxdepth' argument won't work on systems without GNU find, I believe.
You may or may not need it, depending on whether you have subdirectories you
don't want to search.

If that doesn't work, I would most definitely turn to Perl/Ruby/
Python/TCL/.../Java/D/C/C++/C#.

Also, while it's good practice to send stderr to /dev/null,it shouldn't be
at all necessary here. I don't know of any shells which pass stderr through
a pipe unless explicitly directed to do so (e.g., "2>&1")

Chris

On Dec 20, 2007 4:15 PM, Jerry Heiselman <jweida at gmail.com> wrote:

> On 12/20/07, Matthew Nuzum <newz at bearfruit.org> wrote:
> > On Dec 20, 2007 1:58 PM, Major Stubble <major.stubble at gmail.com> wrote:
> >
> > > It is the default shell for Solaris.  Other Unix systems have some
> > > version of it available.  Linux and BSD typically have pfksh, which
> > > to my eyes seems less restrictive than the pure ksh's of the Unix
> world.
> > >
> > > I myself will typically use sh before ksh or bash for most scripts
> > > since it is the most restrictive of all.  That said, I won't work in
> > > a shell environment without my vi keys, so if I am working in bash,
> > > it have to 'set -o vi'.
> > >
> > > All that said, I have never been on a Unix system without ksh.  It is
> > > also true that I have not been on a Linux system without bash.
> > >
> >
>
> It may be beneficial to use the POSIX certified shell on the system,
> which you should always be able to find by setting your PATH to the
> result of `getconf PATH`.  The first shell found will always be POSIX
> compliant and should behave the same on all systems.
>
> That said, I would probably use the solution utilizing the ls *.bad
> 2>/dev/null | wc -l approach.  It does spawn two external programs,
> but the impact in this instance should be pretty negligible.  Of
> course, this runs into a problem on most Solaris machines in that if
> you have more than 250 (I think this is the limit) files, then ls will
> puke because it has an upper limit.  Unfortunately, there really isn't
> a good fix for this except to use another language such as Perl or
> Python.  If you are in this situation, then you will have to go with a
> for loop as it acts more like an iterator.
>
> --
> Jerry H
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cialug.org/pipermail/cialug/attachments/20071220/9b9862b7/attachment.htm


More information about the Cialug mailing list