[comp.unix.shell] Positional parameters beyond $9

slootman@dri.nl (Paul Slootman) (04/16/91)

Hi,
I've just come across a detail of (Bourne) shell programming that I've
not seen before. If a script has more than 9 positional parameters,
there seems to be no way to *directly* access the tenth onwards. Try
this:

$ set a b c d e f g h i j k l m n o p
$ echo $9
i
$ echo $10
a0
$ echo ${10}
sh: bad substitution

Is this a "feature"? The ksh does it ok, but that's not an option for
all the systems we have here. We've done a workaround by assigning to
other variables and shifting, but I was wondering if there isn't any
other way. Nothing in TFM states anything about problems with more
than 9 parameters (unless I've overlooked it...)

FYI, it's on System V systems.

Paul.
-- 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
: slootman@dri.nl           : You are wise, witty and wonderful, but you      :
: ...!hp4nl!dri500!slootman : spend too much time reading this sort of trash. :
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

omerzu@quando.quantum.de (Thomas Omerzu) (04/16/91)

In article <991@dri500.dri.nl> slootman@dri.nl (Paul Slootman) writes:

[...]
>not seen before. If a script has more than 9 positional parameters,
>there seems to be no way to *directly* access the tenth onwards. Try
[...]
>other way. Nothing in TFM states anything about problems with more
>than 9 parameters (unless I've overlooked it...)

Quote from sh(1):

        Parameter Substitution
          The character $ is used to introduce substitutable
          parameters.  There are two types of parameters, positional
          and keyword.  If parameter is a digit, it is a positional
.                                         ^^^^^
          parameter.  Positional parameters may be assigned values by
          set.  Keyword parameters (also known as variables) may be
          assigned values by writing:


digit != number :-)




-- 
*--------------------------------------------------------------------------*
   Thomas Omerzu.       Internet: omerzu@quantum.de
    Quantum GmbH        Bitnet:   omerzu%quando@UNIDO.bitnet
  Dortmund,Germany      UUCP:     ...!unido!quando!omerzu

slootman@dri.nl (Paul Slootman) (04/16/91)

In article <1998@quando.quantum.de> omerzu@quantum.de (Thomas Omerzu) writes:
|>not seen before. If a script has more than 9 positional parameters,
|>there seems to be no way to *directly* access the tenth onwards. Try
|[...]
|>other way. Nothing in TFM states anything about problems with more
|>than 9 parameters (unless I've overlooked it...)
|
|Quote from sh(1):
|
|        Parameter Substitution
|          The character $ is used to introduce substitutable
|          parameters.  There are two types of parameters, positional
|          and keyword.  If parameter is a digit, it is a positional
|.                                         ^^^^^
|          parameter.  Positional parameters may be assigned values by
[...]
|digit != number :-)

OOPS! You're right...

OK, so, there *ISN'T* a way to directly access positional parameters
beyond $9 (besides shifting). :-( I'll stay with my hack, then.

Paul.
-- 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
: slootman@dri.nl           : You are wise, witty and wonderful, but you      :
: ...!hp4nl!dri500!slootman : spend too much time reading this sort of trash. :
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

rhartman@thestepchild.sgi.com (Robert Hartman) (04/17/91)

In article <1998@quando.quantum.de> omerzu@quantum.de (Thomas Omerzu) writes:
>
>In article <991@dri500.dri.nl> slootman@dri.nl (Paul Slootman) writes:
>
>[...]
>>not seen before. If a script has more than 9 positional parameters,
>>there seems to be no way to *directly* access the tenth onwards. Try
>[...]
>>other way. Nothing in TFM states anything about problems with more
>>than 9 parameters (unless I've overlooked it...)
>
>Quote from sh(1):
>
>        Parameter Substitution
>          The character $ is used to introduce substitutable
>          parameters.  There are two types of parameters, positional
>          and keyword.  If parameter is a digit, it is a positional
>.                                         ^^^^^
>          parameter.  Positional parameters may be assigned values by
>          set.  Keyword parameters (also known as variables) may be
>          assigned values by writing:
>
>
>digit != number :-)

There isn't any way to do that directly.  However, I've never
encounterd a case where I wanted to get at $10 without also wanting to
(or being willing to) get at $1-$9.  Here's an example of how I get at
each argument in turn:

while [ $# -gt 0 ] ; do
	case $1 in
	-*) options="$options $1" ;;
	*)  files="$files $1" ;;
	esac
	shift
done

I suppose you could combine this with some sort of test to get at $10:

tmp="$@" #capture args list in temp variable
while [ $# -gt 0 ] ; do
	if [ `expr $# - 10` -eq 0 ] ; then arg10="$1" ; fi
	shift
done
set $tmp #restore original args list

Or you could use your favorite counting look and stick a shift command in
there.  But for handling a variable argument list, I like the first option
best.

-r

greg@cheers.Bungi.COM (Greg Onufer) (04/17/91)

slootman@dri.nl (Paul Slootman) writes:
>OK, so, there *ISN'T* a way to directly access positional parameters
>beyond $9 (besides shifting). :-( I'll stay with my hack, then.

Please note that (at least in the draft I have) POSIX specifies in
its Shells and Utilities standard that ${10} and higher are allowed
and work as expected.  The braces are required, but are optional 
in the single digit case (so scripts that relied on $10 being
${1}0 still work?).

Conclusion?  Get yourself a POSIX-compliant shell.

Cheers!greg