Point to last parameter in BASH command line?

Point to last parameter in BASH command line?

Post by Bob Russe » Wed, 12 Jul 1995 04:00:00



I know this must be easier than I'm making it, but I can't figure out how
to address the last ($N) parameter in a variable length command line.

I am trying to write a script to print only particular pages in a file
such as:  "print 3 4 11 14 test.ps" or "print 9 15 test.ps"

I can't seem to pick up the last parameter to use it in a script line
such as: "lpr2 $1 $N <--- the given filename to print from.

I've gleaned thru all the man and info pages, but I'm missing it somewhere.

TIA

--
|  The *s will continue     |          Bob Russell            |

|                                  |   http://www.veryComputer.com/~bobruss/  |
|         The Management.          |    San Rafael  Marin  CA  USA   |

 
 
 

Point to last parameter in BASH command line?

Post by Michael Sternbe » Wed, 12 Jul 1995 04:00:00



>I can't seem to pick up the last parameter to use it in a script line
>such as: "lpr2 $1 $N <--- the given filename to print from.

[bash]

From the UNIX-FAQ:

for LAST
do
        :
done

The for loop just assigns each of the parms in turn and does a NOP (":").
At the end the last arg is left in the for loop var.

Greetings, Michael
--

Case Western Reserve University         Lab phone: (216) 368 4034
"Who disturrrbs me at this time?"     << Zaphod Beeblebrox IV >>

 
 
 

Point to last parameter in BASH command line?

Post by Andreas Schw » Thu, 13 Jul 1995 04:00:00




|>> >I can't seem to pick up the last parameter to use it in a script line
|>> >such as: "lpr2 $1 $N <--- the given filename to print from.
|>>
|>> From the UNIX-FAQ:
|>> for LAST
|>> do
|>> :
|>> done
|>> The for loop just assigns each of the parms in turn and does a NOP (":").
|>> At the end the last arg is left in the for loop var.

|> I hate to, um, bash Bourne shell compatibility, but this does seem a
|> silly and indirect way to get at the last argument.  Two shells which
|> make this easier are rc and es, where the notation $*($#*) does the job.

|> In rc and es, $var(n) gives the nth element of $var, and $#var is the
|> number of elements in $var.  The shell variable $* holds the command
|> line arguments.  So $*($#*) follows directly.

|> Of course, if you are stuck with bash, this doesn't help.

Well, it does help, if you rewrite it a bit:

LAST="$(eval \${$#})"

--
Andreas Schwab                                      "And now for something

 
 
 

Point to last parameter in BASH command line?

Post by Kevin Woo » Thu, 13 Jul 1995 04:00:00


: I know this must be easier than I'm making it, but I can't figure out how
: to address the last ($N) parameter in a variable length command line.

: I am trying to write a script to print only particular pages in a file
: such as:  "print 3 4 11 14 test.ps" or "print 9 15 test.ps"

: I can't seem to pick up the last parameter to use it in a script line
: such as: "lpr2 $1 $N <--- the given filename to print from.

: I've gleaned thru all the man and info pages, but I'm missing it somewhere.
don't know about bash specifics but since it is sh compatible you
can use:

        eval \$$#

kevin

 
 
 

Point to last parameter in BASH command line?

Post by Chet Ram » Thu, 13 Jul 1995 04:00:00



>I know this must be easier than I'm making it, but I can't figure out how
>to address the last ($N) parameter in a variable length command line.

>I am trying to write a script to print only particular pages in a file
>such as:  "print 3 4 11 14 test.ps" or "print 9 15 test.ps"

>I can't seem to pick up the last parameter to use it in a script line
>such as: "lpr2 $1 $N <--- the given filename to print from.

eval lpr2 $1 \${$#}

The backslash defers the evaluation until `eval' gets hold of the line.
By the time eval runs, the `$#' has already been replaced with the number
of positional parameters, so eval sees something like

        lpr2 3 ${7}

assuming the first argument was `3' and there were 7 arguments to the
script.  You may want to double-quote the arguments if there's a chance
that any of the script arguments will contain characters special to the
shell.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer


 
 
 

Point to last parameter in BASH command line?

Post by Bob Russe » Thu, 13 Jul 1995 04:00:00


: I know this must be easier than I'm making it, but I can't figure out how
: to address the last ($N) parameter in a variable length command line.

   Thank you, everyone, for some useful and informative answers.
I'm pretty new at shell programming, and your responses have given me a
lot of avenues to peruse.

   Reading the man and info pages just don't give any examples, so I get
a bit confused with the wording and what exactly they refer to.


etc. still throws me, but perseverence furthers, to quote the I Ching.

SO, thanks again (I'll be Back, to quote....:)
Bob  
--
|  The *s will continue     |          Bob Russell            |

|                                  |   http://www.veryComputer.com/~bobruss/  |
|         The Management.          |    San Rafael  Marin  CA  USA   |

 
 
 

Point to last parameter in BASH command line?

Post by David W. Tamk » Thu, 13 Jul 1995 04:00:00




| LAST="$(eval \${$#})"

No, but you're close.  This will work in both bash and ksh (and in sh if
there are nine or fewer positional parameters):

        eval LAST=\${$#}

and of course, this does too, as previously suggested (and it also works
in sh):

        for LAST
        do :
        done

 
 
 

Point to last parameter in BASH command line?

Post by Paul Haa » Fri, 14 Jul 1995 04:00:00



> : I know this must be easier than I'm making it, but I can't figure out how
> : to address the last ($N) parameter in a variable length command line.
> don't know about bash specifics but since it is sh compatible you
> can use:
>    eval \$$#

Which works if you have fewer than ten arguments.  In sh, ksh, and bash,
a digit immediately after a $ is expanded directly;  you can't naively
use $10, for example, because it expands to $1 concatenated with a
literal 0.

As Chet said, the workaround for bash (and ksh and POSIX shells) is to
use \${$#};  the ${} notation is not supported by older Bourne shells.

For example:

        bash$ set a b c d e f g h i j k
        bash$ eval echo \$$#
        a1
        bash$ eval echo \${$#}
        k
        bash$

But:

        sh$ set a b c d e f g h i j k
        sh$ eval echo \$$#
        a1
        sh$ eval echo \${$#}
        syntax error
        sh$

Paul

 
 
 

Point to last parameter in BASH command line?

Post by Zefr » Fri, 14 Jul 1995 04:00:00



>I can't seem to pick up the last parameter to use it in a script line
>such as: "lpr2 $1 $N <--- the given filename to print from.

This works in bash, ksh (I think) and zsh, but not sh:


lastparameter="$_"

It's a special case.  You can't get at the second-last parameter this
way.

-zefram

 
 
 

Point to last parameter in BASH command line?

Post by Chet Ram » Mon, 17 Jul 1995 04:00:00


>This works in bash, ksh (I think) and zsh, but not sh:


>lastparameter="$_"


--
``The lyf so short, the craft so long to lerne.'' - Chaucer


 
 
 

Point to last parameter in BASH command line?

Post by Lloyd Zusm » Tue, 18 Jul 1995 04:00:00





> > >I can't seem to pick up the last parameter to use it in a script line
> > >such as: "lpr2 $1 $N <--- the given filename to print from.

> > [ ... ]

> I hate to, um, bash Bourne shell compatibility, but this does seem a
> silly and indirect way to get at the last argument.  Two shells which
> make this easier are rc and es, where the notation $*($#*) does the job.

> [ ... ]

> Of course, if you are stuck with bash, this doesn't help.

I hate to, um, bash es and rc, but when will either of these shells
have job control?  Pretty good shells, otherwise.

--
Lloyd Zusman            01234567 <-- The world famous Indent-o-Meter.

   To get my PGP public key automatically mailed to you, please
   send me email with the following string as the subject or on a
   line by itself in the message (leave off the quotation marks):
                    "mail-request public-key"

 
 
 

Point to last parameter in BASH command line?

Post by Johan Stenbe » Tue, 18 Jul 1995 04:00:00



   > > I hate to, um, bash Bourne shell compatibility, but this does seem a
   > > silly and indirect way to get at the last argument.  Two shells which
   > > make this easier are rc and es, where the notation $*($#*) does the job.
   >
   > I hate to, um, bash es and rc, but when will either of these shells
   > have job control?  Pretty good shells, otherwise.

   Probably never.  I can't speak about rc, but we have said that if
   someone can come up with a small number of primitives to add to es which
   will enable people to write job control as a user-level extension, we'd
   be happy to add them.

   On the other hand, job control is a hard thing to fit into es, where
   shell functions are extremely pervasive;  no shells that I know of
   handle ^Z (SIGSTOP) correctly if it comes in while a complex command or
   shell function is running.

   To give an idea of what I mean, let's take a simple example:

       touch foo; sleep 100; rm foo

   Run this in a job controlling shell.  Type Control-Z (or whatever
   character you use) during the sleep.  What happens?

   I've seen three answers, depending on which shell you use:

       (a) The control-z is ignored.
       (b) The sleep is stopped, and if it is continued (fore- or
           background), the ``rm foo'' is ignored.
       (c) The sleep is stopped and the rm happens right away.

   I've found no existing shell which does:

       (d) The sleep is stopped, and when it is continued, the rm
           is executed.

   To my thinking, (d) is the only correct answer.  I'm pretty sure
   the reason that most shells don't do it is that it's really hard to
   implement correctly, not that anyone thinks that (a), (b), or (c)
   is the right answer.  (My understanding of the state of the art may
   be outdated;  please correct me if I'm wrong and some shell does do
   the right thing.)

[snip]

I would say that (c) is the right behavior. If you want the whole
command to be treated as one job, you should write

        (touch foo; sleep 100; rm foo)

in order for the shell to put in in a subshell and run it. (At least
bash works this way, and if I'm not completely off, so does (t)csh)
If you don't do it this way, redirecting of stdin/stdout/stderr would
become difficult. For example, how would you interpretate a command
like this?

        echo foo; echo bar > myfile

Should myfile contain both foo and bar (the result of the whole
command), or should it only contain bar? (I know, this example
probably would win the `stupidest example award' if there was one,
but it's early in the morning and I haven't had my cup of morning
coffee yet, so....:)

/Stenis
--
**********************************************************************


* Newport Beach CA 92663        * Tel  : +1 714 675 4783 (home)      *
* USA                           *        +1 714 824 5086 (Office)    *
*                               * Fax  : +1 714 824 4056             *
**********************************************************************
There are very few personal problems that cannot be solved through a
suitable application of high explosives.
**********************************************************************

 
 
 

Point to last parameter in BASH command line?

Post by Paul Haa » Tue, 18 Jul 1995 04:00:00



Quote:> > I hate to, um, bash Bourne shell compatibility, but this does seem a
> > silly and indirect way to get at the last argument.  Two shells which
> > make this easier are rc and es, where the notation $*($#*) does the job.

> I hate to, um, bash es and rc, but when will either of these shells
> have job control?  Pretty good shells, otherwise.

Probably never.  I can't speak about rc, but we have said that if
someone can come up with a small number of primitives to add to es which
will enable people to write job control as a user-level extension, we'd
be happy to add them.

On the other hand, job control is a hard thing to fit into es, where
shell functions are extremely pervasive;  no shells that I know of
handle ^Z (SIGSTOP) correctly if it comes in while a complex command or
shell function is running.

To give an idea of what I mean, let's take a simple example:

    touch foo; sleep 100; rm foo

Run this in a job controlling shell.  Type Control-Z (or whatever
character you use) during the sleep.  What happens?

I've seen three answers, depending on which shell you use:

    (a) The control-z is ignored.
    (b) The sleep is stopped, and if it is continued (fore- or
        background), the ``rm foo'' is ignored.
    (c) The sleep is stopped and the rm happens right away.

I've found no existing shell which does:

    (d) The sleep is stopped, and when it is continued, the rm
        is executed.

To my thinking, (d) is the only correct answer.  I'm pretty sure
the reason that most shells don't do it is that it's really hard to
implement correctly, not that anyone thinks that (a), (b), or (c)
is the right answer.  (My understanding of the state of the art may
be outdated;  please correct me if I'm wrong and some shell does do
the right thing.)

The problem with job control for es is that everything just looks
like an es command.  There is no separation *at the language level*
between the internal things and the external ones.  The read-eval-print
loop is written in es itself, so how do you distinguish it from
a shell function?  If you don't implement job control in es as (d),
it just won't work at all, because the whole shell looks like one
big, convoluted shell command.

For more information on es, see

        http://www.webcom.com/~haahr/es/es-usenix-winter93.html

Paul

 
 
 

Point to last parameter in BASH command line?

Post by Steve_Kilba » Tue, 18 Jul 1995 04:00:00



>    On the other hand, job control is a hard thing to fit into es, where
>    shell functions are extremely pervasive;

>    To give an idea of what I mean, let's take a simple example:

>        touch foo; sleep 100; rm foo


Quote:> If you want the whole
> command to be treated as one job, you should write

>    (touch foo; sleep 100; rm foo)

> in order for the shell to put in in a subshell and run it.

I think what Paul meant was that the user had defined a function
which does the touch-sleep-rm sequence, and had then invoked that
function. Normally, the function is treated as a single command
(redirection works, etc.), and therefore, the signal should apply
to the function as a whole, not just the sleep. As another
example, if you take the following function body:
        echo hello ; sleep 10 ; echo goodbye
and you run it, then interrupt it (control-C, or whatever) in the
sleep, you don't see the "goodbye" - the whole function is
interrupted. The same should apply with the control-Z.

The problem is, if you're running a function, which is running
a function, which is running a function (etc), and you control-Z
them, you'd expect all of them to stop. In es, that would also
stop the shell's main interpreter loop, which is just another
function.
--

ISPs are responsible for maintaining the Information Superhighway. They
are not responsible for how you drive.      #include <std_disclaimer.h>

 
 
 

1. Returning LAST command line parameter?

Hi,

    I'm writing a kshell script.

    Let's say, the shell script should echo the last
    parameter:

    foo a b c

    should return "c".

    foo a b c ... z

    should return "z".

    I know that $# returns the NUMBER of parameters,
    so I tried something like "echo ${$#}", but
    unfortunately that returns something else.

    What should the script look like?

    Thomer

2. Quicktime VR?

3. Bash - backslash in a command line parameter

4. wanted: POP3 for Solaris 2.x

5. HELP: re-partition linux !

6. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

7. Hard disk accessing in X windows

8. what sed command to print the first line and the last line

9. bash too many parameters on a commande line !

10. get last char of last positional parameter

11. Passing parameters to command invoked with "bash -c"

12. Last one... bash & missing commands