[nzlug] Re: How to call functions in `find -exec' or `xargs'
Johann Schoonees
j.schoonees at irl.cri.nz
Tue Apr 3 10:19:18 NZST 2007
Jim Cheetham wrote:
>
> > #!/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
OK, that's would work for my small example. In my real application,
myls or MYLS does a lot more work including invoking a nested find.
I suppose there might be some things one simply cannot do (elegantly
anyway) with shell programming.
Johann
PS: There was a mistake in my original example, the pertinent line
should have read:
find . -type f -print0 | xargs -0 myls
The question stays the same though.
This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
More information about the NZLUG
mailing list