Which Shell?


There are at least a dozen Unix shells floating around, but they're all based on two major flavors: Bourne Shell ( sh ) and C Shell ( csh ). The most important shells in the Unix and Linux world are the Bourne Shell, C Shell, Korn Shell (a descendant of C Shell), and Bourne Again Shell ( bash ).

The original command shell of note is the Bourne Shell, written by Steven Bourne at AT&T Bell Labs in the early days of Unix. It's probably still on your Unix box as /bin/sh , and while it's not sexy, and its syntax may be a bit odd, it's a simple and powerful scripting environment so sufficiently common across Unixes that it's the lingua franca of the shell scripting world.

The Free Software Foundation's open source reimplementation of the Bourne Shell goes by the name of bash , the Bourne Again Shell. It is a lot more than just a reimplementation of a 20-year-old command shell, however; it's both a great scripting environment and a highly capable interactive user shell. On many Linux systems, /bin/sh is actually a hard link to bash .

And then there is the C Shell, UC Berkeley's most important innovation in the realm of shell script hacking. The C Shell replaced the odd Bourne Shell syntax with a command syntax more like its namesake language, C.

As with many facets of Unix, passions are strong about which scripting environment is the best, with three predominant camps: Bourne Shell, Korn Shell, and C Shell. But all is not equal. Consider the well-known article "Csh Programming Considered Harmful" [1] whose author, Tom Christiansen, points out, quite correctly:

I am continually shocked and dismayed to see people write test cases, install scripts, and other random hackery using the csh . Lack of proficiency in the Bourne shell has been known to cause errors in /etc/rc and .cronrc files, which is a problem, because you must write these files in that language.

The csh is seductive because the conditionals are more C-like, so the path of least resistance is chosen and a csh script is written. Sadly, this is a lost cause, and the programmer seldom even realizes it, even when they find that many simple things they wish to do range from cumbersome to impossible in the csh .

I agree wholeheartedly with Tom, and hence in this book we will eschew the use of the C Shell. If you're a strong advocate of the C Shell, well, you should find it easy to rewrite almost all of the scripts in this book to fit your shell. Similarly, many people are advocates of the Korn Shell, which has a terrific interactive command line but, I feel, is less capable as a scripting environment.

When evaluating a shell, consider both its interactive capabilities (such as aliases, command-line history, on-the-fly spelling corrections, helpful error messages) and its scripting capabilities. This book focuses on the scripting side of things, and so the scripts presented here will be Bourne Shell scripts (with an occasional sprinkling of bash or POSIX shell tweaks for entertainment value) and should work just fine on any Unix you may have.

The Solaris Factor

If you're working on a Solaris system, you've got a bit of a problem, but not one that can't be solved . The scripts in this book are all written against the POSIX 1003 standard for the Bourne Shell, which includes functions, variable slicing, $() notation as a smarter alternative to backticks, and so on. So what's the problem? The default /bin/sh in Solaris 9 and earlier is not POSIX-compliant , which causes a huge hassle.

Fortunately, you can fix it, in one of two ways:

  1. Replace /bin/sh with a hard link to /usr/xpg4/bin/sh , the POSIX-compliant shell in Solaris. This might be a bit radical , and there's a tiny chance it'll break other things in Solaris, so I'd be cautious about this choice.

  2. In every single script in this book, replace the #!/bin/sh first line with #!/usr/ xpg4/bin/sh , which is straightforward. This has the added advantage of allowing you to automate the process with a for loop similar to the following:

     # This assumes that you're in the  Wicked Cool Shell Scripts  script directory! for script in * do   sed 's#!/bin/sh#!/usr/xpg4/bin/sh' < $script > outfile   mv outfile $script done 

Hopefully, with the release of Solaris 10 Sun will just say "ciao!" to the legacy problems and include a POSIX-compliant version of the Bourne Shell as the default /bin/sh , and this will all go away as a problem.

[1] Online at http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

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