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

Re: Tip of the day: previous command output



On Thu, 19 Aug 2004, Andy Spiegl wrote:

> So I modified Barts "keep" to use
>       kept=$($*)

Unless you also dropped the alias that prefixes keep with noglob, you 
still need $~*, and to make it an array you probably want

	kept=( $($~*) )

> So that I can do
>   keep locate -i pictures | grep -i thursday | grep -i png

I don't know whether you intended this, but [with your original edit] 
that's equivalent to

	kept=$(locate -i pictures)
	print -Rc - $kept | grep -i thursday | grep -i png

whereas I would guess that, rather, you meant

	kept=( $(locate -i pictures | grep -i thursday | grep -i png) )

There's no provision -- except via a zmodload'd module -- for creating a 
user-defined "precommand modifier" like (say) "time" or "coproc" that 
syntatically consumes an entire pipeline.  You'd have to use [inside 
"keep"]

	kept=( $(eval $*) )

[in this case NOT $~*] and write

	keep locate -i pictures \| grep -i thursday \| grep -i png

An alternative is to write "keep" this way:

    keep() {
	kept=()
	kept=( $~* )
	if [[ ! -t 0 ]]; then
	    while read line; do
		kept+=( $line )
	    done
	fi
	print -Rc - $kept
    }

Then you can write

	locate -i pictures | grep -i thursday | grep -i png | keep

and thanks to the magic of zsh running the last command of a pipe in the 
current shell when that command is a function or builtin, you get what you 
want in $kept.



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