[ale] Q: Bash: test for existence of any file matching a glob pattern.

Joe Knapka jknapka at kneuro.net
Sun Aug 8 16:44:03 EDT 2004


Christopher Bergeron <christopher at bergeron.com> writes:

> Joe, are you looking for .h files in the current directory or system
> wide?   If you're just looking for the current directory, try this:
> 
> for f in `\ls *.h`; do echo "doing something to $f; done

This has the same trouble as "for f in *.h" -- it works great
when there *are* files matching the pattern, but it does the
wrong thing when there aren't any.

The answer turns out to be, "set the nullglob shell option,
eg shopt -s nullglob". That changes the behavior of glob
expansion: when nullglob is set, "*.h" always expands to
precisely a list of existing .h files in the current directory
(that is, if there aren't any files matching the pattern,
it expands to "" rather than "*.h").

Doubtless this will come back to bite me in some soft-tissue
area in the future, but for now, it works.

-- 
"We sat and watched as this whole       <-- (Died Pretty -- "Springenfall")
 blue sky turned to black..."
... Re-defeat Bush in '04.
--
pub  1024D/BA496D2B 2004-05-14 Joseph A Knapka
     Key fingerprint = 3BA2 FE72 3CBA D4C2 21E4  C9B4 3230 94D7 BA49 6D2B
If you really want to get my attention, send mail to
jknapka .at. kneuro .dot. net.



More information about the Ale mailing list