Variable substitition

Maarten Litmaath maart at cs.vu.nl
Wed Dec 5 08:30:28 AEST 1990


A slight improvement of my original solution.

# How to get the last argument of a shell script reliably.
# If you are sure the number of arguments is in [1-9], you can use this:
#
#	eval last=\$$#
#
# The general solution works for ANY number of arguments.

set a b c d e f g h i j k l m -n	# For example.

case $# in
0)
	# So the user tried to be funny; let's give him a little surprise.
	exec kill -SYS $$
	;;
[1-9])
	eval last=\$$#
	;;
*)
	last=`
		n=$#
		set '' ${1+"$@"}
		shift $n
		echo -n "$1"
	`
esac

echo "last=|$last|"
--
Q: "Is there a newsgroup for astrology/para-normal?"
A: "Why don't you consult the stars and find out ??"

	--  Phil Watson <pwatson at sunb.mqcc.mq.oz.au>



More information about the Comp.unix.shell mailing list