[comp.unix.questions] /bin/sh

thormod@dr.uucp (Thormod N{ringsrud) (01/12/90)

 Suggestion to enhance /bin/sh
-----------------------------------
I often have a need to pick up the last parameter $1, $2 .. from
the parameter list.  I have tried to catch it by referring $$#
which gives me "processnumber"#.  Then I tried ${$#} which I really
thought would work,  but got instead the message: bad variabel substitution.

What I really want is a new special variabel: $. (dollarpoint) giving
me the last parameter in the list.


	Thormod N{ringsrud
 +-------------------------------------------------------------+
 ! DataRojahn-> Tel: +479876720                Fax: +479876766 !
 ! USENET/EUnet: ...!mcsun!ndosl!dr!thormod or: thormod@dr.uucp!
 +-------------------------------------------------------------+

chris@mimsy.umd.edu (Chris Torek) (01/16/90)

In article <620@dr.uucp> thormod@dr.uucp (Thormod N{ringsrud) writes:
>I often have a need to pick up the last parameter $1, $2 .. from
>the parameter list.

It looks like `eval $"$#"' would do it:

	$ set foo bar baz
	$ eval echo $"$#"
	baz

but for a small problem with sh argument syntax:

	$ set a b c d e f g h i j k l
	$ echo $11
	a1
	$

`$11' means `${1}1', and not ${11}.  (Trying `${11}' directly gives
`bad substitution'.)

The only reliable way to get at the last parameter is something like

	for i do last="$i"; done
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

cpcahil@virtech.uucp (Conor P. Cahill) (01/16/90)

In article <620@dr.uucp>, thormod@dr.uucp (Thormod N{ringsrud) writes:
> I often have a need to pick up the last parameter $1, $2 .. from
> the parameter list.  I have tried to catch it by referring $$#
> which gives me "processnumber"#.  Then I tried ${$#} which I really
> thought would work,  but got instead the message: bad variabel substitution.

The following will work in /bin/sh:

	`eval echo \\\${$#}`

Not easy to type, but it works for shells.
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

maart@cs.vu.nl (Maarten Litmaath) (01/16/90)

In article <620@dr.uucp>,
	thormod@dr.uucp (Thormod N{ringsrud) writes:
\... I often have a need to pick up the last parameter $1, $2 .. from
\the parameter list.  [...]

eval last=\"\$$#\"
-- 
  Q: "How do I convert UNIX files to IXUN format?"  A: "rev | dd conv=swab." |
  Maarten Litmaath @ VU Amsterdam:  maart@cs.vu.nl,  uunet!mcsun!botter!maart

gwyn@smoke.BRL.MIL (Doug Gwyn) (01/16/90)

In article <620@dr.uucp> thormod@dr.uucp (Thormod N{ringsrud) writes:
>What I really want is a new special variabel: $. (dollarpoint) giving
>me the last parameter in the list.

	echo `eval echo $"$#"`

maart@cs.vu.nl (Maarten Litmaath) (01/16/90)

In article <21838@mimsy.umd.edu>,
	chris@mimsy.umd.edu (Chris Torek) writes:
\...
\	$ eval echo $"$#"
\...
\but for a small problem with sh argument syntax:
\
\	$ set a b c d e f g h i j k l
\	$ echo $11
\	a1

A ridiculous feature which should have been fixed long ago...
Similar:
	exec 10< foo 11> bar
-- 
  Q: "How do I convert UNIX files to IXUN format?"  A: "rev | dd conv=swab." |
  Maarten Litmaath @ VU Amsterdam:  maart@cs.vu.nl,  uunet!mcsun!botter!maart