Operating System - HP-UX
1753500 Members
4512 Online
108794 Solutions
New Discussion юеВ

Re: obtaining the variable inside the last arg ($#)

 
SOLVED
Go to solution
u856100
Frequent Advisor

obtaining the variable inside the last arg ($#)

Hi All,

easy one I hope, if I wanted to reference the last argument of the command line inside a script if the parameter list varied, then how do I reference it?

i.e. if the command line was :

script.sh a* c

where the script copies all args from $1 upwards (which represent files i.e. a1 a2 a3 a4, etc), excluding $#

.. and I wanted to reference c as the last parameter on the command line, then ideally I want to reference ${$#}, but I am struggling to escape it properly. My mind has gone blank again!!

hope this makes sense!

thanks in advance
John
chicken or egg first?
4 REPLIES 4
john korterman
Honored Contributor

Re: obtaining the variable inside the last arg ($#)

Hi,
perhaps something like this:
#/usr/bin/sh
NUMBER_1=$(( $# - 1))
shift $NUMBER_1
echo $1


regards,
John K.
it would be nice if you always got a second chance
Francisco J. Soler
Honored Contributor

Re: obtaining the variable inside the last arg ($#)

Hi John,
Another way could be:

for i in $*
do
last=$i
done
echo ${last}


Frank
Linux?. Yes, of course.
john korterman
Honored Contributor
Solution

Re: obtaining the variable inside the last arg ($#)

Hi again,
or this:

LAST=$(echo "$*" | awk '{print $NF}')
echo $LAST

regards,
John K.
it would be nice if you always got a second chance
Nicolas Dumeige
Esteemed Contributor

Re: obtaining the variable inside the last arg ($#)

#!/bin/ksh

last="$#"
eval print \${$last}

exit 0
All different, all Unix