Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do get values from $# 3

Status
Not open for further replies.

mdn6464

Programmer
Sep 22, 2002
4
US
My question is: How do I get the last value of a command line input.

to get the first one would be 1=$1
the second is 2=$2

How do I get the last?

Thanks for your help
 
Hi:

$* or $@ are all the arguments on the command line. For a problem like this, I like to use a function passing all the arguments to the function.

Regards,

Ed


#!/bin/ksh

# function to return the last arg
function lastarg {

for arg in $*
do
:
done
echo $arg

}

lstarg=$(lastarg $*)
echo $lstarg

first=$1
echo $first
second=$2
echo $second
 
Hi
here is the code for you.Hope it will work

if [ $# -gt 0 ]
then
count=`expr $# - 1`
shift $count
echo "The last argument is $1"
fi

Regards
Subbu.
 
Thanks for the advice guys. If I use SHIFT, won't that shift the rest of my inputs away. Meaning I will lose them. I was hoping to process the command line inputs without having to store them anywhere other than the $0-$9.

The last parameter ($#) is the value at which I need to process the rest of the inputs with. I was hoping to get this value first without having to go through the others.

Thanks again for all the help. I'll try storing the inputs elsewhere.

Any other suggestions are welcomed.
 
If you have 10 or fewer arguments (in HP-UX) then eval works.

eval "echo" \$$#

or

eval last_arg=\$$#
echo $last_arg Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Nate:

I didn't know this is why is why you get the star:


eval last_arg=\$$#
echo $last_arg

this successfully works on my Solaris 7 and Red 7.1 systems.

mdn:

You're correct about shift. If you have 9 or less arguments, using Nate's solution works as well my function. Neither solution forces you to store your aguments before finding the last one.

Regards,

Ed
 
Thanks all,
Your help is truely appreciated.

These solutions should get me through the task.
You all have my deepest gratitude.
 
the quick and dirty way to get the last argument...

echo $@ | awk '{ print $NF }'


crowe
 
that's not dirty, quick yes -- but good Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
The awk solution is a great idea but can't take arguments with space characters easily.

The eval solution can take arguments with space characters , i.e. a b "c d" e "f g". Sadly, the number of arguments is limited depending on OS (i.e. 10).

Situations with greater than 10 arguments and arguments with space characters need another solution (c, perl, etc...) Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
In the Korn shell (and probably others)

eval last_arg=\$\{$#\}
echo $last_arg

should work for any number of command line arguments.
 
Hmm, these new suggestions sound interesting. I'll give them a try in my code. Thanks guys.

mdn6464
 
Oops! I didn't have access to a Unix box when I wrote earlier. I got the quoting wrong. It should be

eval "last_arg=\${$#}"
 
Everybody:

I know we've probably beat this thread to death, but if you're running the new korn shell, ksh93, (on Solaris it's referred to as the dest top ksh), according to
Bolsky in his book, the New Kornshell

"${@}" is all positional parameters from 1 on
"${@:3}" is all the positional parameters from 3 on
"${@:3:7}" is all the positional parameters from 3 to 7

I thought I'd see what happens using the argument count, $#:

#!/usr/dt/bin/dtksh

# give me the last argument
echo "${@:$#}"


It works; you don't have to eval or worry about shifting if you're argument count
is over 9.

There's always more than one way of doing things. Too bad some of us have to worry
about portability.

Regards,

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top