Unix Shell - How can I get the last argument of the command line ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > How can I get the last argument of the command line ?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How can I get the last argument of the command line ?
Bo Yang

2006-10-21, 7:25 am

Hi,
Now , I am using the following code to
get the last argument of the command line .

eval echo $`echo $#`

Is there any method more simpler ?

Janis Papanagnou

2006-10-21, 7:25 am

Bo Yang wrote:
> Hi,
> Now , I am using the following code to
> get the last argument of the command line .
>
> eval echo $`echo $#`


You forgot to use quotes

eval echo "$`echo $#`"

>
> Is there any method more simpler ?
>


: "$@"
echo "$_"


Janis
Bruce Barnett

2006-10-21, 1:31 pm

"Bo Yang" <struggleyb@gmail.com> writes:

> Now , I am using the following code to
> get the last argument of the command line .
>
> eval echo $`echo $#`
>
> Is there any method more simpler ?
>



Try
eval echo !$

works with csh/tcsh/bash


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
Chris F.A. Johnson

2006-10-21, 7:31 pm

On 2006-10-21, Bo Yang wrote:
> Hi,
> Now , I am using the following code to
> get the last argument of the command line .
>
> eval echo $`echo $#`
>
> Is there any method more simpler ?


eval "echo \$$#"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Robert Katz

2006-10-22, 1:19 am

Chris F.A. Johnson wrote:
> On 2006-10-21, Bo Yang wrote:
>
> eval "echo \$$#"

eval "echo \${$#}"

--
Regards,

---Robert
Bo Yang

2006-10-22, 1:19 am

Thank you very much ,
Thank you for your reply ,
Thanks !

Rakesh Sharma

2006-10-26, 7:16 am


Bo Yang wrote:
> Hi,
> Now , I am using the following code to
> get the last argument of the command line .
>
> eval echo $`echo $#`
>
> Is there any method more simpler ?
>


for last do :; done
echo "$last"

Alexis Huxley

2006-10-26, 7:16 am

>> eval echo $`echo $#`[vbcol=seagreen]

eval echo \$$#

or if there is any chance that the argument
might have spaces then:

eval echo \"\$$#\"

Alexis
Janis Papanagnou

2006-10-26, 1:15 pm

Alexis Huxley wrote:
>
>
> eval echo \$$#
>
> or if there is any chance that the argument
> might have spaces then:
>
> eval echo \"\$$#\"
>
> Alexis


Both wrong. (Check that code with more than 9 arguments.)

See Robert's posting for a correct version of this pattern.

Janis
bsh

2006-10-27, 1:24 am

Janis Papanagnou wrote:
> Alexis Huxley wrote:
> Both wrong. (Check that code with more than 9 arguments.)


It's good that you pointed that out, but for shells which
cannot accept syntax for command line substitution beyond
the ninth argument (and don't have $_), "${10}" has no better
attribute than exiting gracefully -- which is still important,
BTW!

An idiom that I developed which is both robust _and_
portable is:

eval "set X "\$@"; shift $#"
echo "last argument: $1"

=Brian

Rakesh Sharma

2006-10-27, 1:24 am


bsh wrote:
> Janis Papanagnou wrote:
>
> It's good that you pointed that out, but for shells which
> cannot accept syntax for command line substitution beyond
> the ninth argument (and don't have $_), "${10}" has no better
> attribute than exiting gracefully -- which is still important,
> BTW!
>
> An idiom that I developed which is both robust _and_
> portable is:
>
> eval "set X "\$@"; shift $#"
> echo "last argument: $1"
>
> =Brian



if the arguments have spaces/tabs then the above construct will not
work.

for robustness we need to quote the $@ variable in the set command.
also after running this command we destroy the command line arguments:

***************************************
eval "
set X \"\$@\";shift $#
"
echo "last argument: |$1|"
***************************************


my solution to this post is robust/portable/ n preserves the arguments
too.

--rakesh

bsh

2006-10-27, 7:15 pm

Rakesh Sharma wrote:
> bsh wrote:
> eval "
> set X \"\$@\";shift $#
> "


Ah yes, that's it! I was doing it from memory,
and that last little part I did not remember to
include. Thank you for the correction.

Why we're at it -- and let's see if I can get this
one right from memory -- is the similar technique
useful for doing the usual "shift OPTIND-1" (in
ksh) after a getopts, but in bourne shell. The
usual way is:

shift `expr $OPTIND - 1`

But this has obscene overhead for something
as simple as decrementing a number. So a
10000 times more efficient solution is: (getopts
sets OPTIND to a default of one)

eval "set X \"\$@\";shift $OPTIND"

Which looks familiar, maybe?

=Brian

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com