[nzlug] How to call functions in `find -exec' or `xargs'

Jim Cheetham jim at gonzul.net
Mon Apr 2 16:20:10 NZST 2007


> #!/bin/bash
> function myls ()
> {
>      command ls -1 "$@"
> }
> find . -type f -print0 | xargs -0 myls "$*"
>
>
> $ ./example.sh
> xargs: myls: No such file or directory
> $

Well, the action of xargs or find is to locate an executable to fork
into, and there's no way that they will be able to see the context of
the shell that invoked them (beyond the existance of environment
variables and file descriptors, but you won't be able to get your
hands on them)

The alternative might be to use the shell script itself to construct
the static command you want, storing details in a variable and
invoking exec, instead of using functions.

#!/bin/bash
MYLS="ls -l"
find . -type f -print0 | xargs -0 $MYLS

-jim



More information about the NZLUG mailing list