zsh-users
 help / color / mirror / code / Atom feed
From: Kurtis Rader <krader@skepticism.us>
To: TJ Luoma <luomat@gmail.com>
Cc: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: how do I get the last argument from a list of arguments?
Date: Mon, 7 Jul 2014 14:33:05 -0700	[thread overview]
Message-ID: <CABx2=D_F7iJwtVFHR9U77AtAojtgpAZQ0xQt7pOqWbCNQwK9PQ@mail.gmail.com> (raw)
In-Reply-To: <CADjGqHuALx7Ue3Ei3s7XvT_pHWxECo2rt=BNegPO5Fwcuge2LQ@mail.gmail.com>

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

See the description of the *${name:offset} *syntax in *man zshexpn*. Note
the space between the colon and minus-sign is required to disambiguate it
from the "var:-default" syntax.

function lastarg() {
    print "${@[-1]}"
    print "${@: -1}"
}

lastarg 1 2 3


On Mon, Jul 7, 2014 at 2:06 PM, TJ Luoma <luomat@gmail.com> wrote:

> I’m trying to learn better ways of dealing with arguments given to a
> function, because I am sure that I am not doing it the most efficient
> way.
>
> For example, if I want to process a series of args, I usually use a
> loop like this:
>
> for FOO in "$@"
> do
>      case "$FOO" in
>           -t|--to)
>                shift
>                TO="$1"
>                shift
>           ;;
>
>           -v|--verbose)
>                VERBOSE='yes'
>           shift
>           ;;
>
>           -*|--*)
>                echo " $NAME [warning]: Don't know what to do with arg: $1"
>                shift
>           ;;
>
>      esac
>
> done # for args
>
> That has worked OK for what I've needed to do, but now I'm trying to
> create two functions which I will use in place of 'cp' and 'mv' and I
> need to be able to find the _last_ argument (the destination) before I
> process all the rest of the args.
>
> The only way that I can think of to get the last argument is to do
> something like this
>
>      LAST=`echo "$@" | awk '{print $NF}'`
>
> but that made me wonder if there wasn’t a better way.
>
> Note that this does not need to be 'portable' at all -- I will happily
> use any zsh-specific features which may exist, as long as it works in
> 5.0.2 (which is what comes with OS X).
>
> Thanks!
>
> TjL
>



-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

  reply	other threads:[~2014-07-07 21:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <luomat@gmail.com>
2014-07-07 21:06 ` TJ Luoma
2014-07-07 21:33   ` Kurtis Rader [this message]
2014-07-07 21:35   ` Peter Stephenson
2014-07-07 22:06   ` Bart Schaefer
2014-07-08 12:52   ` zzapper
2014-07-08 14:11     ` zzapper
2014-07-08 15:06       ` Roman Neuhauser
2014-07-08 15:25         ` zzapper
2014-07-08 15:42           ` Roman Neuhauser

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABx2=D_F7iJwtVFHR9U77AtAojtgpAZQ0xQt7pOqWbCNQwK9PQ@mail.gmail.com' \
    --to=krader@skepticism.us \
    --cc=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).