zsh-users
 help / color / mirror / code / Atom feed
* Globbing in a function (was Re: globbing for links in pathnames)
@ 1999-02-05 23:48 Sweth Chandramouli
  0 siblings, 0 replies; 2+ messages in thread
From: Sweth Chandramouli @ 1999-02-05 23:48 UTC (permalink / raw)
  To: zsh-users

On Thu, Feb 04, 1999 at 09:07:55PM -0800, Bart Schaefer wrote:
> On Feb 4, 11:22pm, Sweth Chandramouli wrote:
> } so it should end up something like the following:
> } 
> } x=(pkgs/*(@)) ; ls -d ${^x}/man(/)
> 
> The reason I used the eval was to avoid having x remain set after the
> command completes.  `x=(*(@)) eval ...` will unset x again at the end
> of the eval, and won't destroy any existing value of x.

	i have a function that i use to link executables from where i
normally install them to (usually /opt/wherever) to /usr/local/bin.
i was trying to rewrite it using this sort of format (rather than
invoking find, as i was doing before), but am running afoul of something
that i can't quite figure out.  the i've hacked out some stuff at the
beginning (to allow me to pass it options) for the sake of clarity; the 
interesting part is this:

(astaroth)~/.zfunc: BINLIST=(*(*)) ; echo $BINLIST ; unset BINLIST
binlink cwhois gpid mck newm newterm nobrs perf xbuffy xt xtitle
(astaroth)~/.zfunc: which binlink
binlink () {
        BINLIST=($1(*)) eval 'for BIN in ${=BINLIST} ; do 
      ln -s ${BIN} /usr/local/bin ;
   done'
}
(astaroth)~/.zfunc: binlink \*                                    
zsh: no matches found: *(*)

	putting an echo in at the end confirms that BINLIST in
the function is, when binlink is given an escaped asterisk as
its argument, being set to (*(*)); since parameter expansion
occurs before filename generation, why doesn't setting BINLIST
to that value in the function do the same thing that it does
from the command line?

	-- sweth.

-- 
Sweth Chandramouli
IS Coordinator, The George Washington University
<sweth@gwu.edu> / (202) 994 - 8521 (V) / (202) 994 - 0458 (F)
<a href="http://astaroth.nit.gwu.edu/~sweth/disc.html">*</a>


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

* Re: Globbing in a function (was Re: globbing for links in pathnames)
@ 1999-02-06  4:27 Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1999-02-06  4:27 UTC (permalink / raw)
  To: Sweth Chandramouli, zsh-users

On Feb 5,  6:48pm, Sweth Chandramouli wrote:
} Subject: Globbing in a function (was Re: globbing for links in pathnames)
}
} (astaroth)~/.zfunc: which binlink
} binlink () {
}         BINLIST=($1(*)) eval 'for BIN in ${=BINLIST} ; do 
}       ln -s ${BIN} /usr/local/bin ;
}    done'
} }
} (astaroth)~/.zfunc: binlink \*                                    
} zsh: no matches found: *(*)

Remember what I said a couple of postings ago about parameter expansion?
When a parameter is replaced by its value, the string is not tokenized.
Thus BINLIST=($1(*)) is effectively the same as BINLIST=("*"(*)).  If
you use the ~ flag on the parameter expansion, tokenization is done and
you'll get the glob you expect.

Also, you don't need the = in ${=BINLIST} because BINLIST is already an
array.  If you ever try to binlink a file with spaces in its name, that
extra word-split will bite you.

Finally, "ln -s" isn't going to do the right thing unless you give it a
full path (or one relative to /usr/local/bin, which could be computed if
you really work at it, but a full path is easier).

So, change the function to

    binlink () {
	BINLIST=($~1(*)) eval 'for BIN in $BINLIST ; do 
	    ln -s $PWD/$BIN /usr/local/bin ;
	done'
    }

and it should work.

However, a better way to write that particular function would be:

    binlink () {
        argv=($^~==*(*))
	ln -s "$PWD/$^@" /usr/local/bin
    }

Can you figure out what that's doing?

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


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

end of thread, other threads:[~1999-02-06  4:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-05 23:48 Globbing in a function (was Re: globbing for links in pathnames) Sweth Chandramouli
1999-02-06  4:27 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).