ksh keeps up

Unix Insider –

You're a shell programmer, right? Do you know how far that can take you?

If you use ksh93, you can travel roughly as far as you could with any other scripting language.

You already know it

Most Unix workers have done at least simple command-line scripting with login shells such as

<font face="Courier">sh</font>
,
<font face="Courier">bash</font>
,
<font face="Courier">tcsh</font>
, and so on. Perhaps you've needed the names of the 10 files in a particular directory that were most recently changed, and you wrote:

<font face="Courier">
      ls -1td $DIRECTORY | head -10
   </font>

If you know that much about programming the shell, then you're ready to do complicated arithmetic, network applications, and even GUIs. That's the promise that ksh93 fulfills.

<font face="Courier">ksh</font>
is the Korn shell, named after AT&T Labs researcher David Korn. Korn has been working on
<font face="Courier">ksh</font>
for almost 20 years.
<font face="Courier">ksh</font>
is upward-compatible with the Bourne shell (
<font face="Courier">sh</font>
), it has essentially all the capabilities of other popular login shells, it was a standard for SysVr4 Unix, and it was the reference for the POSIX and ISO shell definitions. It's also faster than other shells, sometimes as much as by an order of magnitude, and it has functionality that makes it a full-fledged language, on a par with Perl, Tcl, and other newcomer languages.

What was the holdup?

So why aren't you already using

<font face="Courier">ksh</font>
for everything you do? Probably not for technical reasons.
<font face="Courier">ksh</font>
has always been owned by AT&T and other companies (various combinations of Lucent and Novell, mostly). Its license was onerous, compared with those of Perl, Tcl, and Bash. This hindered its adoption in many cases.

Last year, however, Korn succeeded in opening up the source code, and now ksh93 is available in both source and binary forms under a liberal AT&T license. This promotes Korn's positioning of KornShell (the language that ksh93 implements) as a peer of Perl, Python, and so on. "We don't write anything in Perl anymore, because [ksh93] has all the functionality built-in," Korn claims.

That's strictly true only for a restricted meaning of all, of course; there's no

<font face="Courier">ksh</font>
equivalent to CPAN, which supplies all Perl's add-on functionality in Web service, database management, and other specific areas. ksh93 also doesn't support programming in the large with the namespaces, object orientation, and exception handling of other advanced scripting languages. However, anyone accustomed to shell programming who needs to manage networks, build GUI control panels, integrate C-coded modules, and write maintainable applications should look into ksh93.
<font face="Courier">ksh</font>
is nearly unique, of course, in offering all this power from a processor that is also usable as a login shell.

Portable shell

ksh93 is freely available for essentially all Unixes, and also, with light license restrictions, for Windows and MVS. A handful of vendors support ksh93 commercially. Korn's team at AT&T plans to upgrade a future release of KornShell with a combination of object inheritance, support for binary objects, and namespaces. Also for that release, Korn writes, "Multithreading is a possibility, but less likely than the others."

Korn has even made much of the contents of his authoritative reference book on ksh available for download. Here's an example fragment, which gives an idea of ksh's syntax and functionality:

<font face="Courier">
      # Set SECONDS to number of seconds since midnight.
      export SECONDS="$(date '+3600*%H+60*%M+%S')"
      # The following variables store hours and minutes.
      typeset -Z2 _h _m       # Two columns, leading zeros.
      # The following expression reformats SECONDS.
      _time='$((_h=(SECONDS/3600)%24)):$((_m=(SECONDS/60)%60))'
      # Use _time within PS1 to get the time of day.
      PS1="($_time)"'!$ '
      # Note that $_time gets replaced by above expression.
      # Expression gets evaluated when PS1 is displayed.    
   </font>

This example is particularly interesting as an example of language evolution. In 1999, after the book was published,

<font face="Courier">ksh</font>
's
<font face="Courier">printf</font>
operation was augmented to include new time capabilities. A consequence is that all the code above can now be abbreviated to the single line:

<font face="Courier">
      PS1='$(printf "(%(%H:%M)T)!$ " now)'
   </font>

Ruby threads

Our mention last month of several distinct models for handling threading detailed the specifics for only a few scripting languages. Ruby fans felt left out. Here's Ruby's story:

Ruby supports multithreading as a programming construct, even under such operating systems as DOS where none is native. Ruby's cooperative multithreading has been implemented in terms of green threads with considerable care and has a prominent place in well-styled Ruby programs. The standard library includes useful Mutex and ConditionVariable helper classes, which help structure multithreaded applications. Ruby takes pains to avoid blocking on system input/output functions. On the other hand, Ruby doesn't scale. It doesn't take direct advantage of symmetric multiprocessing (SMP) hardware. Moreover, it relies on standard libraries for heap management; these are notorious for their poor scalability. However, Ruby appears to be in a better position than older languages to improve these performance bottlenecks, should it prove important. Among the merits of the book Programming Ruby is that it explains Ruby's thread programming quite clearly.

Scripting awards

ActiveState Tools is one of several companies built on open source scripting languages. To promote this connection and return a bit of attention to the communities that support these languages, ActiveState sponsored a competition this winter to name top Perl and Python programmers. Congratulations to Tim Vroom, Christian Tismer, Nick Ing-Simmons, and Greg Ward. See the Resources section below for the press release describing their accomplishments.

Resources and Related Links

Top 10 Hot Internet of Things Startups
Join the discussion
Be the first to comment on this article. Our Commenting Policies