shells?

Marcus Hall marcus at illusion.uucp
Wed Apr 17 04:25:41 AEST 1991


In <1455 at irit.irit.fr> pfeiffer at irit.fr (Daniel Pfeiffer) writes:
+Does some shell just provide improvements over Bourne shell.  Given
+the amount of code that exists for Bourne shell this should be upwards
+compatible.  For example I heard that [ test ] is a built in for ksh.
+Does that mean we have we can say (without ;):
+
+if [ test ] then
+while [ test ] do

In article <1991Apr16.050922.22662 at ux1.cso.uiuc.edu> jeffo at uiuc.edu writes:
=Actually, I think what you say in KSH is: (the preferred notation)
=
=if [[ expression_to_be_run ]] then
=while [[ expression_to_be_run ]] then
=
=The 'test' notation and the [ notation (according to the KSH book) have
=been made obsolete by the [[ ]] notation.

Most current day Bourne shells have a built-in test.  There acutally is
a good reason to continue using "["; it is more portable to write in the
subset of ksh that the Bourne shell supports.

+The only things I miss in Bourne shell is a list type or something
+like that, so I can get at ${27} or ${$#} for command line args, and
+for similar but separate structures of my own.  And it would be nice
+if it could do some clever redirection for builtins and functions in
+pipelines, rather than fork off a subshell for each one.  Then we
+wouldn't have to bend over backwards to set variables, cd and other
+things.

=Sounds like KSH is for you!  You can do all that, but ${$#} is the PID
=of the command, and not the number of arguments to the command ($# is
=this).  I recommend you get the KSH and the book by Morris I. Bolsky
=and David G. Korn called the "The Kornshell Command and Programming
=Language" (ISBN 0-13-516972-0).  It's a great book and even though I've
=only had my copy for about 3 weeks, I've already gained a world of
=knowledge from it.

Well, you can get ${$#}, but you have to use eval to get it.  This would
echo the last argument:
	eval echo \${$#}

The reason that ${$#} gives you the PID is that ${var#pattern} is being
used where var is "$" and pattern is "".  So, "" is matched (and deleted)
from the start of $$ and the result is the process ID.

It is possible to do a form of subscripting in ksh (and sh as well) by
playing with eval.  Thus, if you set xx_1="one", xx_2="two", xx_3="three",
i=2, and execute:
	eval echo \$xx_$i
you will get "two" output.  This works for any alphanumeric index.

Finally, ksh will run loops and functions with simple redirection in the
current process, but pipes cause it to create a child process.  Thus it
still requires the bending over backwards to get variables back from the
loop/function.

marcus hall



More information about the Comp.unix.shell mailing list