I need to invoke find on a set of "starting points" which is generated, but some of paths may not be valid:
paths() {
#
# mock version of the generator
#
echo /bin
echo kjsdfhk
echo /etc
}
find $(paths) -type f name foo.sh
My problem is that I don't know if the path will be valid and if it's not, I want to silently ignore it. Easiest for me is now
paths \
| while read path;
do
test -e "$path" || continue
find "$path" -type f -name foo.sh
done
but this is expensive: it invokes find for each valid path, and since the whole code may be called inside loop, I'd like to find a more effective way.
One easy and very unix-y solution would be to have find read the "starting points" from STDIN:
paths \
| while read path;
do
test -e "$path" || continue
done \
| find - -type f -name foo.sh
except that... find does not support this! :)
Any ideas?
Note that the paths are provided by user, so spaces (and probably other funny things) need to be considered. Also I'm aiming for POSIX /bin/sh
but that could be sacrificed. (Oh, and sinking whole STDERR to /dev/null
is not an option either...)
Update: Sorry, I forgot to mention that I'm restricted to bash---I probably confused it more by the POSIX note. Actually the code is looking for snippets to source inside Bash scripts and is now mostly in sh with few bashisms that I might get rid of in future version. So if I could avoid putting more bashisms in, that would be cool but I certainly can't afford "zshisms", however elegant --- as @stephane's answer (go vote it up now!).
test -d
) paths to a shell array and pass that array tofind
as argument.find
, has to be a directory