Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: script within find



On Tue, Sep 11, 2007 at 09:52:19AM +0200, Alexy Khrabrov wrote:
> A while ago I asked about generalization of the nested command in find
> -exec.  What if I have to execute a random series of shell commands
> for each file found by find and given to exec -- how should I package
> the series under zsh?  I tried -exec (cmd1; cmd2; cmd3) and it seems
> not to work -- find complains about ( and ) and loses ; ...  Is it a
> shell-sensitive problem or find-specific?
[...]

Like any other shell. Here, it's down to find syntax, not zsh
syntax.

So

find ... -exec cmd1 \; -exec cmd2 \; -exec cmd3 \;

But beware cmd2 will only be executed if cmd1 succeeds (returns
a non-null return code).

Or;

find ... -exec sh -c '
   cmd1
   cmd2
   cmd3' \;

That's an inline script. If you want to pass arguments to that
script beware that the first one will be available as $0, not
$1. It's not true for very old or non standard shell, that's why
you sometimes find:

find ... -exec sh -c '
  cmd1 -- "$1"
  cmd2 -- "$1"' {} {} \;

Or if you want to user the + syntax to pass more than one file
path at the same time:

find ... -exec sh -c '
  shift "$1"
  for file
  do cmd -- "$file"
  done' 2 1 {} +

But with modern shs, you should be able to do:

find ... -exec sh -c '
  cmd1 -- "$0"
  cmd2 -- "$0"' {} \;

find ... -exec sh -c '
  for file
  do cmd -- "$file"
  done' inline {} +

But with zsh, you generally no longer need find as zsh's
globbing can do almost everything find can do.

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author