zsh-users
 help / color / mirror / code / Atom feed
* running a command multiple times on a glob pattern
@ 2001-09-06  1:23 Andre Pang
  2001-09-06  6:41 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Andre Pang @ 2001-09-06  1:23 UTC (permalink / raw)
  To: zsh-users

hello,

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

(ignore the silly syntax i'm using, of course).  is this
possible at all?  i think it's used frequently enough that it
might be worthwhile to implement.


-- 
#ozone/algorithm <ozone@algorithm.com.au>          - trust.in.love.to.save


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: running a command multiple times on a glob pattern
  2001-09-06  1:23 running a command multiple times on a glob pattern Andre Pang
@ 2001-09-06  6:41 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2001-09-06  6:41 UTC (permalink / raw)
  To: Andre Pang, zsh-users

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   


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-09-06  6:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-06  1:23 running a command multiple times on a glob pattern Andre Pang
2001-09-06  6:41 ` Bart Schaefer

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).