On Mon, Sep 20, 2010 at 09:14:15AM -0400, Greg Wooledge wrote:

> The disadvantage of -exec is that if you wanted to do something within
> your shell (putting the filenames into an array, incrementing a counter
> variable, etc.), you can't.  You're already two processes removed from
> your shell.  Likewise, you can't -exec a shell function that you wrote.
> You would have to use a separate script, or write out the shell code in
> quotes and call -exec sh -c '....'.

Although sh -c '' is not the same shell instance/environment anymore,
I would keep it in mind as very useful and flexible in many cases.


> For example, if we wanted to do vi `find . -name '*.c'` but actually have
> it WORK in the general case, we end up needing this monstrosity:
> 
> unset array
> while IFS= read -r -d '' f; do array+=("$f"); done \
>   < <(find . -name '*.c' -print0)
> vi "${arr...@]}"
> 
> ... which uses three bash extensions and one BSD/GNU extension.  To the
> best of my knowledge, the task is completely impossible in strict POSIX.
> (You can work around the -print0 by using -exec printf '%s\0' {} + but
> then there's no way to read the NUL-delimited stream, and no arrays
> to put it into, as you cannot set positional parameters individually.)

As you mention -exec yourself, what about simply

find . -type f -name '*.c' -exec sh -c 'vi "$@"' find-sh {} +

Reply via email to