Re: function inside find



2008-12-31, 03:13(-08), marc:
Is it possible to call an own function inside find ?

I've tried :

find /home/foo -type f -mtime -1 -exec copy {} \;

where copy is a function defined before, but it is never called.
[...]

The function is only known to the shell where you declare it, so
not to find not even to a shell that you may tell find to run.

You'd need to do something like:

find /home/foo -type f -mtime -1 -exec sh -c '
copy() {
cp -- "$@" /dest
}
copy "$@"' sh {} +

For instance.

But of course, you don't need to define the function.

find /home/foo -type f -mtime -1 -exec sh -c '
cp -- "$@" /dest' sh {} +

is enough.

With bash and some version of ksh, you can export functions:

copy() {
cp -- "$@" /dest
}
export -f copy
find /home/foo -type f -mtime -1 -exec bash -c '
copy "$@"' bash {} +

--
Stéphane
.



Relevant Pages

  • FAST, but full feature ksh.
    ... built ksh vs a statically built bash, I found that the ksh was ... sluggish bash to the POSSIBLY quicker ksh... ... case of molasses, try playing with shell scripting on cygwin, ... but today my measurement now isn't done ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Linux users seeks some freeBSD pointers
    ... >any freely available ksh versions. ... I can't because the few times I've tried bash I did not like it. ... referring to ksh93 which was first released over six years ... tcsh is an acceptable shell for interactive use ...
    (comp.unix.bsd.freebsd.misc)
  • [gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.25)
    ... This is the Bash FAQ, version 3.25, for Bash version 2.05b. ... Bash, the GNU Bourne-Again Shell. ... C3) Which new features in ksh-93 are not in bash, ... D1) Why does bash run a different version of `command' than ...
    (comp.unix.questions)
  • [gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.26)
    ... This is the Bash FAQ, version 3.26, for Bash version 2.05b. ... Bash, the GNU Bourne-Again Shell. ... C3) Which new features in ksh-93 are not in bash, ... D1) Why does bash run a different version of `command' than ...
    (comp.unix.questions)
  • [gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.25)
    ... This is the Bash FAQ, version 3.25, for Bash version 2.05b. ... Bash, the GNU Bourne-Again Shell. ... C3) Which new features in ksh-93 are not in bash, ... D1) Why does bash run a different version of `command' than ...
    (comp.unix.questions)