evaluating ${10} and above in sh/ksh

Gordon C. Galligher gorpong at ping.uucp
Tue Aug 14 13:36:14 AEST 1990


In article <514 at risky.Convergent.COM> chrisb at risky.Convergent.COM 
(Chris Bertin) writes:
>There doesn't seem to be a way, in sh or ksh, to evaluate $10 and higher.
>$10 and higher are evaluated as ${1}0, ${1}1, etc...
>		     instead of ${10}, ${11}, etc...
>I have tried many different ways and they all fail. Do you know one
>that will work?

I realize this is hardly what you wish to read, but the only way to gain
access to the others is by using 'shift' to get them.  You could, on the
other hand set up a loop to grab all of them into your own variables:

	argv0=$0
	start=1
	pred=argv
	for i
	do
		eval "${pred}${start}='$i'"	# Be sure to quote the $i !!
		start=`expr $start + 1`
	done
	argc=`expr $start - 1`

This would give you variables $argv0, $argv1, $argv2 up to and including
$argv$argc.  To simply print them out:

	echo "	0 - $argv0"
	start=1
	while [ $start -le $argc ]
	do
		echo "	$start - `eval echo '$'${pred}${start}`" # Yes, ugly
		start=`expr $start + 1`
	done

No, it is not pretty, but it is workable.  There may not be many times you
would want to print option 'x' without knowing what 'x' was.  If you
always wanted to check the first option, then it is just $argv1.  If you
find yourself always parsing through the command line options, then you
should probably put a case inside of the for and then do with them what
you will.

		-- Gordon.
-- 
Gordon C. Galligher	9127 Potter Rd. #2E	Des. Plaines, Ill.	60016
     telxon!ping%gorpong at uunet.uu.net (not tested)  (Is this even legal??)
     ...!uunet!telxon!ping!gorpong      (tested)    (And it works!)
"It seems to me, Golan, that the advance of civilization is nothing but an



More information about the Comp.unix.wizards mailing list