Finding the last arg
Dan Mercer
mercer at npdiss1.StPaul.NCR.COM
Wed Jan 2 10:42:51 AEST 1991
In article <1990Dec27.154917.14751 at virtech.uucp> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
:In article <18476 at shlump.nac.dec.com> lan_csse at netrix.nac.dec.com (CSSE LAN Test Account) writes:
:>The obvious thing to try is some sort of expression combining $# with ${},
:>e.g. ${$#}. This gets a syntax error. The simpler $$# is valid, but it
:
:Try:
: eval \$$#
:
:--
:Conor P. Cahill (703)430-9247 Virtual Technologies, Inc.,
:uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160
: Sterling, VA 22170
Obviously this fails for $# > 9. So you need some form of shift,
preferably forking and execing as few processes as possible (nice to
avoid expr). You'd also like to preserve the current args. So try
this:
args="$@" # get arglist
argno=$# # and number of current args
export args argno
last=`set -- spacer $args # set as args - add spacer to eliminate expr
shift $argno # shift over
echo $1`
This works if args contains no args with embedded whitespace - if that
is a possibility, then expr must be used.
args="$@"
export args argno
last=` set -- spacer $args
shift \`expr $# - 1\`
echo $1`
echo $last
But this also fails if last arg may contain whitespace. Oh well!
--
Dan Mercer
NCR Network Products Division - Network Integration Services
Reply-To: mercer at npdiss1.StPaul.NCR.COM (Dan Mercer)
"MAN - the only one word oxymoron in the English Language"
More information about the Comp.unix.shell
mailing list