zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Andre Pang <ozone@algorithm.com.au>, zsh-users@sunsite.dk
Subject: Re: running a command multiple times on a glob pattern
Date: Thu, 6 Sep 2001 06:41:13 +0000	[thread overview]
Message-ID: <1010906064113.ZM15455@candle.brasslantern.com> (raw)
In-Reply-To: <999739397.219670.15579.nullmailer@bozar.algorithm.com.au>

On Sep 6, 11:23am, Andre Pang wrote:
} 
} i'm thinking that it'd be really handy to have some sort of
} shell construct that looks like
} 
}     tar zxvf {{*.tar.gz}}
} 
} which basically expands to
} 
}     for i in *.tar.gz; do tar zxvf $i; done

There are all sorts of ways to do this.  Here's one:

    setopt short_loops
    alias with='for WITH in'
    function call { emulate -L zsh; $* $WITH }

(Pick different words than "call" and "with" if you like.)  Now you write

    with *.tar.gz; call tar zxvf

The drawback being that it doesn't nest, because there's only one loop
variable.  As you can tell, short_loops are pretty terse to begin with;
it's only two more characters to write

    for i in *.tar.gz; tar zxvf $i

but do whatever floats your boat.  Another possibility is:

    function call {
	emulate -L zsh
	local a=$'\0\1'
	a=$argv[(I)$a]		# $'...' doesn't work in subscripts
	if (( a )); then
	    local -a cmd args
	    cmd=($argv[1,a-1])
	    args=($argv[a+1,-1])
	    for a in $args; do $cmd $a; done
	fi
    }
    alias -g with=$'\0\1'

(Use a string more unlikely than $'\0\1' if you find it necessary.)  Now
you can say

    call tar zxvf with *.tar.gz

and if you need to pass "with" as an argument, you just quote it.

    zsh% call echo with this "with" that
    this
    with
    that

You can even nest this, though it doesn't work precisely as you might
expect:

    zsh% call call echo this and that with those and with some more
    this and that those
    this and that and
    this and that some
    this and that those
    this and that and
    this and that more

I could go on, but I won't.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


      reply	other threads:[~2001-09-06  6:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-06  1:23 Andre Pang
2001-09-06  6:41 ` Bart Schaefer [this message]

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=1010906064113.ZM15455@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=ozone@algorithm.com.au \
    --cc=zsh-users@sunsite.dk \
    /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).