zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Mildly interesting zargs trick: call an anonymous function
Date: Tue, 11 Feb 2014 22:39:09 -0800	[thread overview]
Message-ID: <140211223909.ZM24888@torch.brasslantern.com> (raw)

Leonardo Barbosa's question got me thinking:  What if the number of files
is so large that "rm" chokes with "argument list too long" or the like?

Then you want to use zargs to split the list into managable chunks:

    zargs -- **/*.tex(.:s/.tex/.aux) -- rm

However, there's only so much stuff you can pack into the recursive glob
before it begins to get unweildy.  So if it were more complicated than
this example, you could define a function to do the heavy work.

    rm_aux() { rm ${*:s/.tex/.aux} }
    zargs -- **/*.tex(.) -- rm_aux

That lets us use the array tricks without creating a global array, but
leaves the function rm_aux hanging around.  Wouldn't it be nice to use
one of those "anonymous" functions supported by recent zsh?

    ## DO NOT DO THIS:
    zargs -- **/*.tex(.) -- (){ rm ${*:s/.tex/.aux} }

Oops, we've just defined zargs, --, and all our .tex file names to be
functions that execute "rm".  That's no good, we have to quote the
anonymous function.

    ## THIS SPEWS "command not found":
    zargs -- **/*.tex(.) -- '(){ rm ${*:s/.tex/.aux} }'

    ## BUT DO NOT DO THIS EITHER:
    zargs -- **/*.tex(.) -- eval '(){ rm ${*:s/.tex/.aux} }'

That seems fine, but if any of the .tex files has special characters
or whitespace in its path name, that "eval" may do horrible things.
Fortunately, there's a simple glob qualifier that will quote all of
that stuff for us.

    ## I AM PRETTY SURE THIS ONE IS OK, BUT NO WARRANTY:
    zargs -- **/*.tex(.:q) -- eval '(){ rm ${*:s/.tex/.aux} }'

There are of course other ways to slice up this particular example.

    zargs -- **/*.tex(.:rq) -- eval '(){ rm ${^*}.aux }'

Other variations left as exercises for the reader.


                 reply	other threads:[~2014-02-12  6:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140211223909.ZM24888@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).