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

Christopher Bergeron christopher at bergeron.com
Sun Aug 8 16:19:13 EDT 2004


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

(backtick backslash ls star period h backtick)


Best regards,
CB


Joe Knapka wrote:

>Hi folks,
>
>A simple Bash question that google, the Bash FAQ, and the
>comp.unix.shell FAQ have failed to answer. (Possibly man bash would
>answer it, but I find I usually fall asleep before finding the
>particular tidbit I'm looking for in that horrendously gigantic
>manpage. Plus I expect there are folks on this list who do this
>kind of thing six times before breakfast.)
>
>I need to do this:
>
>if (any file matching *.h exists); then do something; fi
>
>The "any file matching *.h" test is defeating me. No variety of "[ -e
>... ]" seems to work (quoting the "*.h" different ways, and so
>forth). I've also tried
>
>for f in *.h ; do something to $f ; done
>
>That works except when there are *not* any *.h files, in which case it
>binds f to the literal string "*.h" instead, which causes bad stuff to
>happen in the "do something to $f" part. Since I'm doing this inside
>of a generic rule in a common make file for a development tree, I
>really need the command to work properly even in directories that
>don't contain any *.h files (in which case it should simply do
>nothing, not cause an error condition).
>
>Any assistance is appreciated.
>
>Thanks,
>
>-- Joe
>
>  
>



More information about the Ale mailing list