zsh-users
 help / color / mirror / code / Atom feed
* brace expansion in function
@ 2004-06-13  1:51 Eric Smith
  2004-06-13  4:31 ` Bart Schaefer
  2004-06-13 16:04 ` Thomas Köhler
  0 siblings, 2 replies; 8+ messages in thread
From: Eric Smith @ 2004-06-13  1:51 UTC (permalink / raw)
  To: zsh-users

I would like a shell function that expands the arg
{corrs,dylan,crespo}

This try only gives all corrs songs:

mpg() {
mpg321 *$1*
}

thanks

-- 
Eric Smith


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

* Re: brace expansion in function
  2004-06-13  1:51 brace expansion in function Eric Smith
@ 2004-06-13  4:31 ` Bart Schaefer
  2004-06-13 12:53   ` Eric Smith
  2004-06-13 16:04 ` Thomas Köhler
  1 sibling, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2004-06-13  4:31 UTC (permalink / raw)
  To: zsh-users

On Sun, 13 Jun 2004, Eric Smith wrote:

> I would like a shell function that expands the arg
> {corrs,dylan,crespo}

Do you mean that you have typed

	mpg {corrs,dylan,crespo}

?? If that's what you did, then the shell will have expanded the braces
before invoking the function, so it is the same as if you'd typed

	mpg corrs dylan crespo

(which is actually fewer characters, so I'm not sure why you didn't just
type that directly) in which case what you want is something like

	mpg() { mpg321 *${^@}* }

If instead you mean that you typed

	mpg '{corrs,dylan,crespo}'

or some such quoting, then you need

	mpg() { eval "mpg321 *$1*" }

If none of this looks reasonable, you'll have to ask a more detailed 
question.


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

* Re: brace expansion in function
  2004-06-13  4:31 ` Bart Schaefer
@ 2004-06-13 12:53   ` Eric Smith
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Smith @ 2004-06-13 12:53 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer said:
> 
> 	mpg corrs dylan crespo
> 
> (which is actually fewer characters, so I'm not sure why you didn't just
> type that directly) in which case what you want is something like
> 
> 	mpg() { mpg321 *${^@}* }
> 

yummy.

-- 
Eric Smith


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

* Re: brace expansion in function
  2004-06-13  1:51 brace expansion in function Eric Smith
  2004-06-13  4:31 ` Bart Schaefer
@ 2004-06-13 16:04 ` Thomas Köhler
  2004-06-14  7:37   ` ignore globs that do not match Eric Smith
  1 sibling, 1 reply; 8+ messages in thread
From: Thomas Köhler @ 2004-06-13 16:04 UTC (permalink / raw)
  To: Eric Smith; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 549 bytes --]

Eric Smith wrote:
> I would like a shell function that expands the arg
> {corrs,dylan,crespo}
> 
> This try only gives all corrs songs:
> 
> mpg() {
> mpg321 *$1*
> }

mpg() {
    local arg
    local l
    for arg ; do l="*$arg* $l" ; done
    eval mpg321 $l
}

Ciao,
Thomas

-- 
 Thomas Köhler       Email:       jean-luc@picard.franken.de
     <><             WWW:           http://jeanluc-picard.de
                     IRC:                           tkoehler
                     PGP public key available from Homepage!

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* ignore globs that do not match
  2004-06-13 16:04 ` Thomas Köhler
@ 2004-06-14  7:37   ` Eric Smith
  2004-06-14  8:31     ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Smith @ 2004-06-14  7:37 UTC (permalink / raw)
  To: zsh-users

Thomas Köhler said:
> mpg() {
>     local arg
>     local l
>     for arg ; do l="*$arg* $l" ; done
>     eval mpg321 $l
> }
> 

Nice!

But how might I still hear dylan and crespo if I typed:

$ mpg dylan gibberish crespo

("setopt cshnullglob" does not seem to work.)

-- 
Eric Smith


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

* Re: ignore globs that do not match
  2004-06-14  7:37   ` ignore globs that do not match Eric Smith
@ 2004-06-14  8:31     ` Bart Schaefer
  2004-06-22  6:56       ` ignore globs that do not match -> prevent function from exiting Eric Smith
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2004-06-14  8:31 UTC (permalink / raw)
  To: zsh-users

On Mon, 14 Jun 2004, Eric Smith wrote:

> Thomas Köhler said:
> > mpg() {
> >     local arg
> >     local l
> >     for arg ; do l="*$arg* $l" ; done
> >     eval mpg321 $l
> > }
> > 
> 
> Nice!

Almost equivalent to, but more wordy than, my *${^@}* solution.  The only
difference is that e.g.:

	mpg 'd*n'

(note quotes) evals "mpg321 *d*n*" and therefore expands the middle "*"
as well.  It wasn't clear you wanted those semantics.

> ("setopt cshnullglob" does not seem to work.)

That would be the way.  Works when I try it, with either formulation of
the function.

> $ mpg dylan gibberish crespo

Are you sure there are no globbing characters in the gibberish?  The
setopt will only apply inside the function, e.g. if you say

$ mpg foo*bar

then the shell first attempts to glob foo*bar and either gets "no match"  
or calls the mpg function with the results as arguments.

Perhaps what you're after is something like:

	mpg_glob() { mpg321 *${~^@}* }
	alias mpg='noglob mpg_glob'


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

* Re: ignore globs that do not match -> prevent function from exiting
  2004-06-14  8:31     ` Bart Schaefer
@ 2004-06-22  6:56       ` Eric Smith
  2004-06-22  7:17         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Smith @ 2004-06-22  6:56 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer said:
> 	mpg_glob() { mpg321 *${~^@}* }
> 	alias mpg='noglob mpg_glob'

If the globbed argument list includes an invalid file or something that
causes mpg321 to fall over, then mpg_glob() exits.

How could I avoid this?

thanks
-- 
Eric Smith


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

* Re: ignore globs that do not match -> prevent function from exiting
  2004-06-22  6:56       ` ignore globs that do not match -> prevent function from exiting Eric Smith
@ 2004-06-22  7:17         ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2004-06-22  7:17 UTC (permalink / raw)
  To: zsh-users

nOn Tue, 22 Jun 2004, Eric Smith wrote:

> Bart Schaefer said:
> > 	mpg_glob() { mpg321 *${~^@}* }
> > 	alias mpg='noglob mpg_glob'
> 
> If the globbed argument list includes an invalid file or something that
> causes mpg321 to fall over, then mpg_glob() exits.

Hmm, sorry, I didn't consolidate all the other fragmentary answers I gave
into that final example.  It should be

	mpg_glob() {
	  setopt localoptions cshnullglob
	  mpg321 *${~^@}*
	}
	alias mpg='noglob mpg_glob'


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

end of thread, other threads:[~2004-06-22  7:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-13  1:51 brace expansion in function Eric Smith
2004-06-13  4:31 ` Bart Schaefer
2004-06-13 12:53   ` Eric Smith
2004-06-13 16:04 ` Thomas Köhler
2004-06-14  7:37   ` ignore globs that do not match Eric Smith
2004-06-14  8:31     ` Bart Schaefer
2004-06-22  6:56       ` ignore globs that do not match -> prevent function from exiting Eric Smith
2004-06-22  7:17         ` 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).