The Design Philosophy of UNIX


Several things have kept UNIX going strong even after 30 years. One of these is its portability, as mentioned earlier. One widely held opinion among its adherents, however, is that the most important thing that has kept UNIX on the cutting edge, when most other software that old is considered obsolete, is its design philosophy dictating how an operating system should work. Many people tend to think of UNIX as an extremely complicated, austere, and confusing operating system. But to seasoned users, UNIX is the ultimate example of the KISS (Keep It Simple, Stupid) credo of software engineering. This enables savvy users to perform very complex tasks with rudimentary tools, as long as they tie together the right way.

The UNIX design philosophy is that every task should have its own small software program that performs that one task and does it well. But the designers of UNIX had a brilliant ideathat these programs should be able to be combined together by the user to do things that a single program could not do by itself.

Note

This combination concept is known as piping. Doug McIlroy of Bell Labs is credited with coming up with the idea. Ken Thompson implemented it in UNIX. (We cover pipes in detail as part of Chapter 8, "Working with the Shell.")


Here is an example of the way these "pipes" of data processing programs work. Suppose that you have a plain-text file that serves as a simple address book. It uses one line per person and contains names, addresses, phone numbers, email addresses, and so on. Fields in this file are separated by a tilde (~), a character unlikely to be used in any of the data fields. A few sample lines from the file might look like these:

Doe, John~505 Some Street~Anytown~NY~55555~505-555-1212~jdoe@email.com Doe, Jane~121 Any Street~Sometown~NY~12121~121-555-1212~jadoe@isp.com Bar, Foo~501 Some Street~Anytown~NY 55555~505-123-4567~foobar@email.com


This file could contain 50 names or 500 namesit really doesn't matter. You want to be able to mine and rearrange the data in the file to generate several different results: to get a list of all the people that live in Anytown; to get just their names and phone numbers; to sort the list alphabetically; and to print out a hard copy of the list. There is no single command that will do everything you want, but you can combine several commands together in a pipe to do what you want. Here is one of several ways that this task could be accomplished:

# awk 'BEGIN {FS="~"} $3 == "Anytown" {print "%s\t%s\n",$1,$6}'\ address.txt |sort |lp


Don't worry if you don't understand exactly what this code does; it's explained in Chapter 10, "Shell Programming." In simple terms, the code sets the field separator to the tilde, selects lines where the third field (the field that contains the city name) is equal to Anytown, and then prints the first and sixth fields (the name and phone number) of these lines, separated by tabs (\t), with a newline at the end of each line (\n). The file it gets the information from is address.txt. The output is then piped to the sort command, which sorts it in alphabetical order. It is then piped to the lp command, which will print the output on the default printer. Here is what the output looks like using our simple three-record data file:

Bar, Foo        505-123-4567 Doe, John       505-555-1212


Although this command string may seem somewhat arcane to a novice, a UNIX user with only a little bit of experience can decode it easily and tell what it's meant to do. The single line of code shown here creates a simple database that, with a few judicious modifications, can be made to search by any field and present output in any form you want.

Just so you can see how powerful this single line of code can be, here is a second example that modifies the previous example slightly to print a simple mailing list:

# awk 'BEGIN {FS="~"} $3 == "Anytown"\ {printf "%s\n%s\n$s, $s $s\n\n",$1,$2,$3,$4,$5}' address.txt |lp


The output of the preceding code is as follows:

Doe, John 505 Some Street Anytown, NY 55555 Bar, Foo 501 Some Street Anytown, NY 55555


Being able to reduce complex functions like these to a single command line is critical to writing shell programs, as you'll see in Chapter 10; with just a few lines made up of these rudimentary UNIX commands, you can create deceptively simple scripts that perform very useful tasks, and which you can modify to suit any need. For those who are willing to learn, UNIX is about as close to an infinitely customizable and flexible operating system as you can get. Immense power is locked up inside UNIX (and therefore FreeBSD) that can be unleashed and used to do things for free that you might expect to require expensive, dedicated software.

It is the flexibility of this timeless design philosophy, retaining its usefulness no matter how fast computer hardware becomes or how complex desktop GUIs get, that has kept UNIX relevant, unlike so many other software efforts over the years.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net