I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1).
The following code doesn't yield any result:
function foo {
echo "Hello World"
}
find somedir -exec foo \;
The find does match several files (as shown by replacing -exec ... with -print), and the function works perfectly when called outside from the find call.
Here's what the man find page say about -exec:
-exec command True if the executed command returns a
zero value as exit status. The end of
command must be punctuated by an escaped
semicolon (;). A command argument {} is
replaced by the current pathname. If the
last argument to -exec is {} and you
specify + rather than the semicolon (;),
the command is invoked fewer times, with
{} replaced by groups of pathnames. If
any invocation of the command returns a
non-zero value as exit status, find
returns a non-zero exit status.
I could probably get away doing something like this:
for f in $(find somedir); do
foo
done
But I'm afraid of dealing with field separator issues.
Is it possible to call a shell function (defined in the same script, let's not bother with scoping issues) from a find ... -exec ... call?
I tried it with both /usr/bin/find and /bin/find and got the same result.
export -f fooPATH. Alternatively, usesh -c '...'and both define AND run the function in the...bit. It may help to understand the differences between functions and scripts.