ProTech Professional Training Services UNIX Shell Programming
My Solution To The "Killer" Lab

Most training materials provide short segments of code as examples. While that is helpful initially learning features, sometimes to fully comprehend how the features are used, a more comprehensive example is needed. This script attempts to be that comprehensive example. The lab exercise was expanded (feature creep) to include additional functions, better error checking etc. The result is a complete program that uses most features of the Korn shell, using the ksh88 feature set. Using ksh93 features would simplify some of the code, but not in a dramatic way.

Although there are lots of comments in the code, sometimes it may be necessary to analyze individual lines of code to see how they really work.

This example illustrates that sometimes it is necessary to be very creative, and to take full advantage of utilities such as sed and awk, to achieve the desired results.

At least it is possible to see how the features of the shell are combined to achieve some things that initially might seem impossible or impractical.

Functions are defined before they are used, so calls tend to be to a preceding location in the script. This means the calling program itself is at the end.

The lab exercise is to write a script that maintains a simple database of names, telephone numbers and email addresses. The exercise specifies using a "flat" file, with colons as field delimiters (like the /etc/passwd file). This is a reasonable choice, since there are unlikely to be colons in the data.

Obvious functions are to add, list, modify and delete records. Although these requirements sound simple, it is also necessary to be able to find a particular record to avoid duplicating records and to find the record to modify or delete.

The lab exercise intends that a user interact with a menu to use the program. This example expands on that to allow command line changes as well.

A personal database is unlikely to be more than a few thousand entries. If it was, the program would probably be coded in C++ or Java, and the data stored in a database accessed with SQL. So a "flat" file is a reasonable approach.

The lab exercise intends that the entire database be read into arrays, manipulated and saved on exit. This requires quite a lot of array data manipulation for searching etc. and makes it difficult to take advantage of utilities such as sed.

For this example solution, the database is manipulated as a file, using regex to search for records with sed. This approach avoids the complexity of loading, sorting and saving arrays of data.

Every change is saved immediately to disk, avoiding the coding of ensuring the data is saved when an abort occurs. To ensure the data is not lost when an error occurs, when data is changed the entire database is written to a new file. The original database is moved to a backup and then the updated database is moved in place of the original. An error at any point means that a good copy of the original database is available.

This disk-based approach is a trade-off of cpu time for array manipulation versus disk storage. In either case, a user interacting with the script is unlikely to tax modern UNIX systems to any degree :-)

In the spirit of providing a useful example, a number of additional things were included that were not part of the original requirements of the lab exercise:

This example illustrates the use of the following, using Korn shell (1988) features:

I hope you find this example educational !

IMPORTANT: When you copy and paste the source code to your computer, tabs will become spaces.
Generally this makes no difference, but FIX LINES 590 and 598, which say
IFS="     "
Replace those spaces with a single tab, or importing from a tab delimited file will fail !
Rename the file to killer_lab.ksh


[Qenesis Home] - [UNIX Training] - [Shell Programming]

Copyright © 2014 Qenesis Inc. All rights reserved.
All trademarks belong to their respective owners.