Finding the last arg

Jeff Beadles jeff at onion.pdx.com
Thu Dec 27 17:09:03 AEST 1990


In <18476 at shlump.nac.dec.com> lan_csse at netrix.nac.dec.com writes:


...
>The problem is that I can't figure out any Bourne-shell expression that
>gives the last argument.
...
>Any ideas?


Well, there's nothing that I am aware of in /bin/sh that will ALWAYS allow
this to work.  The problem is that if you have more than 9 arguements, you
have to shift them off to get to what you want.  Here's one try that might
be of use:


#!/bin/sh

if [ $# -gt 0 -a $# -lt 10 ] ; then
	LAST=`eval echo \\$$#`		## 1-9 == Great!
elif [ $# -le 0 ] ; then
	LAST="NO_ARGS"			## 0 == Oops! None!
else
	LAST="TOO_MANY_ARGS"		## >9 == Oops!  Too many!
fi

echo "LAST= >$LAST<"
exit 0

However, this is not all that swift.  "foo a b c d e f g h i j k" won't work
with this.

The only other hope is to save off the arguements and loop until the end.  This
has the possibility of screwing up any special characters and/or white space
though, and is generally not-so-hot.


	-Jeff
-- 
Jeff Beadles		jeff at onion.pdx.com



More information about the Comp.unix.shell mailing list