zsh-users
 help / color / mirror / code / Atom feed
* passing arg to function / alias
@ 2011-07-17 19:29 TJ Luoma
  2011-07-17 21:42 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: TJ Luoma @ 2011-07-17 19:29 UTC (permalink / raw)
  To: Zsh Users

I am trying to figure out how to pass optional arguments to a shell
script and then have them be passed on to another program.

I'm not sure I can explain this well, so I'm going to try to
demonstrate what I mean with an example.

Imagine this is a program called 'list.zsh':

	#!/bin/zsh -f

	LS_ARG="-l"

	case "$@" in
		-j)
					LS_ARG="-t -r $LS_ARG"
					shift
		;;

		-z)
					LS_ARG="-R -t $LS_ARG"
					shift
		;;
	esac

	eval /bin/ls ${LS_ARG} $@

	exit 0

In this example, I want 'list.zsh' to ALWAYS do 'ls -l' but

* if I add '-j' I want it to do the equivalent of 'ls -l -t -r'
* if I add '-z' I want it to do 'ls -l -R -t'

Ideally, since /bin/ls has a lot of other flags, I would like to be
able to do this too:

./list.zsh -j -z -s /path/to/dir

and have "-s" just "passed along" to "/bin/ls" as another argument.

('list.zsh' is just for example purposes. I'm trying to figure out how
to do this same thing for several different programs, including curl
and lynx and others.)

I thought that if I looped through the arguments using 'case' and
appended each argument to the variable 'LS_ARG' then:

The results are pretty terrible.

list.zsh pretty much only works if I don't give it any arguments or if
I just give it one of -z or -j. If I give it two (as in "list.zsh -j
-z" or even "list.zsh -z -S"), 'ls' complains that j/z are illegal
arguments.

Is this even possible?

If so, what am I doing wrong?

Thanks for your time.

TjL


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

* Re: passing arg to function / alias
  2011-07-17 19:29 passing arg to function / alias TJ Luoma
@ 2011-07-17 21:42 ` Bart Schaefer
  2011-08-08 20:34   ` TJ Luoma
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2011-07-17 21:42 UTC (permalink / raw)
  To: Zsh Users

On Jul 17,  3:29pm, TJ Luoma wrote:
} Subject: passing arg to function / alias
}
} I thought that if I looped through the arguments using 'case' and
} appended each argument to the variable 'LS_ARG' then:
} 
} The results are pretty terrible.

"case" doesn't loop by itself, you need to call it inside one.  Also
in zsh you probably want to use an array rather word splitting.
Something like

    local -a LS_ARG
    while [[ $# -gt 0 && "$1" = -* ]]
    do case "$1" in
       -j) LS_ARG=(-t -r $LS_ARG);;
       -z) LS_ARG=(-R -t $LS_ARG);;
       *) LS_ARG=($LS_ARG $1);;
       esac
       shift
    done

    /bin/ls -l $LS_ARG $@

However, you might want to look at the "getopts" builtin if you want
to write this in a POSIX-portable way, or at the "zparseopts" builtin
for a zsh-specific alternative.


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

* Re: passing arg to function / alias
  2011-07-17 21:42 ` Bart Schaefer
@ 2011-08-08 20:34   ` TJ Luoma
  0 siblings, 0 replies; 3+ messages in thread
From: TJ Luoma @ 2011-08-08 20:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On Sun, Jul 17, 2011 at 5:42 PM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> "case" doesn't loop by itself, you need to call it inside one.

Oops! I actually knew that but neglected it in my example

> Also
> in zsh you probably want to use an array rather word splitting.
> Something like
>
>    local -a LS_ARG
>    while [[ $# -gt 0 && "$1" = -* ]]
>    do case "$1" in
>       -j) LS_ARG=(-t -r $LS_ARG);;
>       -z) LS_ARG=(-R -t $LS_ARG);;
>       *) LS_ARG=($LS_ARG $1);;
>       esac
>       shift
>    done
>
>    /bin/ls -l $LS_ARG $@

AHA! An array worked perfectly for me. Thanks!

> However, you might want to look at the "getopts" builtin if you want
> to write this in a POSIX-portable way, or at the "zparseopts" builtin
> for a zsh-specific alternative.

I decided to go "all in" on zsh for my scripting, so I'm not worried
about "portability" (not that it's not a bad goal, just one that I've
chosen not to worry about given finite resources of time and attention
and memory, since these are for personal use.)

That said 'zparseopts' has gone into my list of "ZSH Things To Learn"
but for now, arrays for everyone!

Thanks!

TjL

ps - sorry for the reply-delay. I've only now had a chance to go in
and try this. Where did July go? How is the summer almost over
already!?


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

end of thread, other threads:[~2011-08-08 20:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-17 19:29 passing arg to function / alias TJ Luoma
2011-07-17 21:42 ` Bart Schaefer
2011-08-08 20:34   ` TJ Luoma

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