The New KornShell—ksh93

The KornShell, written by David Korn at Bell Telephone Laboratories, combined the best features of both of these shells, and added the ability to edit and reenter the current and previous commands using the same keystrokes as either the vi or the Emacs editor as the user desired.

The Unix system was one of the first systems that didn't make the command interpreter a part of the operating system or a privileged task. It was written as an ordinary user process with no special permissions or calls to unadvertised functions. This has led to a succession of better and better shells. The early generations of Unix came with a command shell written by Ken Thompson, one of the inventors of the Unix system. By the late 1970s, two vastly improved shells emerged. The Bourne shell, created by Steve Bourne at Bell Telephone Laboratories, was a big improvement as a language. The C shell, created by Bill Joy at the University of California at Berkeley, was a much improved command interpreter but a poor language.

The KornShell, written by David Korn at Bell Telephone Laboratories, combined the best features of both of these shells, and added the ability to edit and reenter the current and previous commands using the same keystrokes as either the vi or the Emacs editor as the user desired. This shell became very popular, but its distribution was restricted. As a result, several freely available imitations such as pdksh and bash were created. An enhanced version of C shell, tcsh, was created to provide visual editing to C shell users.

While the Bourne shell provided a good basis for programming, and this was improved upon by earlier versions of KornShell, it was not adequate for general purpose scripting without combining it with other languages such as the awk programming language. While in most instances the two languages work well together, the performance penalty of using two languages with separate processes is often prohibitive. The Perl language was created to provide a single language with the combined functionality of the shell and awk. However, Perl has a syntax that many find difficult to understand.

ksh93, the latest major revision of the KornShell language provides an alternative to Tcl and Perl. As a programming language, it has comparable speed and functionality to each of these languages, yet is arguably the best interactive shell. It is a superset of the POSIX 1003.2 shell standard. Like Tcl, it is extensible and embeddable with a C language application programming interface. In fact, two graphical shells have been created using ksh93. One of these, dTksh, is a Motif-based language developed by Novell. The other, Tksh, written by Jeff Korn at Princeton University, uses the Tk library, and is briefly discussed here.

The best way to describe the new features found in ksh93 is to illustrate them through an example. We will create a shell script named lsc, shown in Listing 1, to provide an ls output with subdirectory names printed in bold. We will need to maintain the multi-column output associated with the standard ls.

The lsc script will produce the ls output for each directory name provided as a command line argument. The default action is to produce the ls output for the current directory. Several modifications can be made to the lsc script for enhanced performance. We leave them as an exercise for the reader. We perform the following high level actions for each directory name to be processed.

for each directory do

load directory entries into array entries
load entries
calculate number of columns in multi-column output
calculate maximum number of rows
print the current directory name
determine output layout
add entries to row[] array
add entries to col[] array
calculate the column widths
display the output

done

Arrays

ksh93 provides one-dimensional indexed and associative arrays. An array element is referenced as varName[subscript]. Indexed arrays use arithmetic expressions for subscripts. This permits computation within the subscript expression. The statement varName[3+8] for example, references the 11th element of the indexed array. (Arithmetic expressions are described more fully below).

The elements of an indexed array can be initialized from a list using the varName=(....) command. This provides a convenient notation for initializing an array to contain the names of files in a given directory. The number of entries in the array describes the number of files found. As an example, consider the following statement to initialize the entries indexed array with the names of files found in the current directory: entries=(*)

An associative array uses arbitrary strings for subscripts. We could, for example, create a state tax associative array and reference elements by the state name. This works even for space separated tokens within the string, such as New Jersey.

typeset -A StateTax
StateTax[New Jersey]=0.06
print ${StateTax[New Jersey]}

Several special positional parameter expansions are provided for array processing. Using ${varName[@]} refers to all elements of the array. The subscripts of an array can be referenced with ${!varName[@]}. The notation ${#varName[@]} provides the number of elements within the array. Elements within a numeric subscript range can be referenced using ${varName[@]:offset:length}. This special notation works with both indexed and associative arrays.

Arrays are used throughout the example lsc script. We define video as an associative array with capability names from the terminfo database as subscripts. The definition of video is provided as a compound assignment for an associative array.

video=(
   [bold]=$(tput bold)
   [reset]=$(tput reset)
   [reverse]=$(tput reverse)
)

Each element is assigned a value from the standard output of a tput execution for the capability name. For example, video[bold] is the terminfo sequence for bold lettering. Similarly, video[reverse] will provide reverse video output.

Using the notation $(command) will cause command to execute in a subshell of the current ksh. In many instances, ksh will not actually fork/exec a subshell when command is a built-in or a shell function. (Built-in functions are described below).

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState