Shells


The shell is an application that reads user input and reports system or program output. The shell is the primary way a user communicates with a Unix system, and is the primary way the system communicates with a user. Although the kernel does most of the work, the shell is the part that is visible to the user. Various shells are available, such as sh (standard Unix Bourne shell), csh (Berkeley Unix c-shell), and ksh (Korn shell). The Bourne shell, sh, is generally useful in shell scripting, which is discussed further in the next section. For interactive work, or in Unix parlance for login sessions, a more mature featured shell is generally desired. In previous versions of Mac OS X, the default login shell was tcsh, which is a significantly enhanced version of csh. With Panther, the default login shell is bash. Bash is the GNU Project’s Bourne Again Shell, which has the same syntax as sh but with features similar to tcsh and zsh, such as command-line editing, job control, brace expansion, and a command history similar to csh. More importantly, bash is the default shell in most Linux distributions. Having the same shell on Mac OS X machines is a great boon for both Linux and Macintosh users. Often, there will be more than one machine on a network, and by becoming familiar with shell usage on the Macintosh, you’ll know your way around a Linux system for free.

Tip

For more information on bash, the project’s homepage has all the information.

A Mac OS X installation provides five shells in the standard distribution: sh, csh, tcsh, bash, and zsh. Shells fall into two general camps: sh derivatives and csh derivatives. These camps don’t differ by much superficially, but they do some things differently. For example, csh derivatives use setenv to set environment variables while sh derivatives use a combination of set and export to set these variables.

As mentioned previously, sh is generally used for shell scripts. The same goes for csh, although the beginning shell scripter should be warned against using csh for shell scripting. For further information on that topic please read “Csh Programming Considered Harmful” by Tom Christiansen, available at www.faqs.org/faqs/unix-faq/shell/csh-whynot/. Of interest is that although both sh and csh are available on a Mac OS X system, in fact sh is a copy of bash and csh is a copy of tcsh. Both bash and tcsh behave slightly differently when called by their other names (sh or csh).

In many respects, you can think of the different shells as different dialects of the same language. After you learn one shell, you can pretty much communicate in any of the shells; however, you occasionally find differences in syntax, and you may not command the full power of the new shell’s enhancements. For example, if you are fully proficient in every aspect of csh, all of that knowledge is usable in bash. But to get the full power of bash, you can take advantage of its command history, command substitution, and other enhancements. Command history lets you recall previous commands using various keyboard shortcuts; Command substitution lets you reuse previous commands in new instances. Moving to zsh, you would learn different techniques for some of these enhancements.

Some common special characters

Unix originated in an era of extremely limited memory and disk space. Conservation of resources was critical; therefore, command names were abbreviated. Commands and file names were case-sensitive, which provided more diversity in short names and eliminated the need to provide code-parsing commands into a consistent case for comparisons.

Additionally, a number of special and punctuation characters were employed as shortcuts or abbreviations. Some are shown in the following list:

~

home directory

.

current directory

..

parent directory to the current directory

/

topmost or root directory

Scripting the shell

All provided shells support shell scripting. A shell script is simply a text file containing a list of commands that the shell will execute in order. A shell script supports many programmatic constructs such as variables, conditionals, and loops, as well as supporting a number of control and redirection operators.

Perhaps you left out the .doc extension on a folder of Microsoft Word files that you want to share with a Windows-using colleague. You can write a very simple shell script to rename all the files in a directory. Your script contains a list of instructions to the shell, using all the constructs listed here. It holds the text string .doc in a variable, and uses a loop to first create a list of all files in the directory, then to append the variable to each item in the list. Then it writes a logfile using output redirection. That is to say that, rather than putting output on the screen, as a command is likely to do, output is redirected to a file. The shell provides a control function that enables you to run this program in the background while you do other things, rather than wait for the program to complete before resuming your own operations.

You can find entire books devoted to Unix shell scripting. All we can give you here are the basics to get you started and pointers to where you can find more information. Read on; later in the Chapter, we’ll return to this topic, but first we’ll need to cover a bit more background. (The man pages for sh, csh, bash, and tcsh are a good place to begin if you are curious.)

Note

The AppleScript Script Menu shown in Chapter 23 will run shell and perl scripts as well as AppleScripts. In addition to shell scripting and AppleScript language interpreters, Mac OS X 10.3 ships with Perl, PHP, Python, Ruby, and Tcl.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

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