ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT

ksh keeps up

Unix Insider 3/16/01

Cameron Laird and Kathryn Soraiz, Unix Insider

unixinsiderhome
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.

On this topic

You already know it
Most Unix workers have done at least simple command-line scripting with login shells such as sh, bash, tcsh, 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:


      ls -1td $DIRECTORY | head -10
   

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.

ksh is the Korn shell, named after AT&T Labs researcher David Korn. Korn has been working on ksh for almost 20 years. ksh is upward-compatible with the Bourne shell (sh), 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 ksh for everything you do? Probably not for technical reasons. ksh 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 ksh 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. ksh 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:


      # 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.    
   

This example is particularly interesting as an example of language evolution. In 1999, after the book was published, ksh's printf operation was augmented to include new time capabilities. A consequence is that all the code above can now be abbreviated to the single line:


      PS1='$(printf "(%(%H:%M)T)!$ " now)'
   

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

Cameron Laird and Kathryn Soraiz manage Phaseit, a software consultancy just outside Houston.




Sponsored Links

Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
IP Networks Boost Secure Health Communications
AT&T provides secure communication to keep health care moving forward.
Protecting the Enterprise Network Through Web Security
New focus is being placed on securing Web-based threats.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Enterprise IP Goes Mobile
To maximize full productivity, companies must integrate their mobile applications with the IP network.
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Application Development  Systems and network programming  Shell Programming  KornShell
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.