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

Re: running a command multiple times on a glob pattern



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   



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