< Day Day Up > |
1.2. Definition and Function of a ShellThe shell is a special program used as an interface between the user and the heart of the UNIX/Linux operating system, a program called the kernel, as shown in Figure 1.1. The kernel is loaded into memory at boot-up time and manages the system until shutdown. It creates and controls processes, and manages memory, file systems, communications, and so forth. All other programs, including shell programs, reside on the disk. The kernel loads those programs into memory, executes them, and cleans up the system when they terminate. The shell is a utility program that starts up when you log on. It allows users to interact with the kernel by interpreting commands that are typed either at the command line or in a script file. Figure 1.1. The kernel, the shell, and you.When you log on, an interactive shell starts up and prompts you for input. After you type a command, it is the responsibility of the shell to (a) parse the command line; (b) handle wildcards, redirection, pipes, and job control; and (c) search for the command, and if found, execute that command. When you first learn UNIX/Linux, you spend most of your time executing commands from the prompt. You use the shell interactively. If you type the same set of commands on a regular basis, you may want to automate those tasks. This can be done by putting the commands in a file, called a script file, and then executing the file. A shell script works much like a batch file: A list of UNIX/Linux commands is typed into a file, and then the file is executed. More sophisticated scripts contain programming constructs for making decisions, looping, file testing, and so forth. Writing scripts not only requires learning programming constructs and techniques, but also assumes that you have a good understanding of UNIX/Linux utilities and how they work. There are some utilities, such as grep, sed, and awk, that are extremely powerful tools used in scripts for the manipulation of command output and files. After you have become familiar with these tools and the programming constructs for your particular shell, you will be ready to start writing useful scripts. When executing commands from within a script, you are using the shell as a programming language. 1.2.1 The UNIX ShellsThe three prominent and supported shells on most UNIX systems are the Bourne shell (AT&T shell), the C shell (Berkeley shell), and the Korn shell (a superset of the Bourne shell). All three of these behave pretty much the same way when running interactively, but have some differences in syntax and efficiency when used as scripting languages. The Bourne shell (sh) is the standard UNIX shell, and is used to administer the system. Most of the system adBministration scripts, such as the rc start and stop scripts and shutdown are Bourne shell scripts; this is the shell commonly used by the administrator when running as root. This shell was written at AT&T and is known for being concise, compact, and fast. The default Bourne shell prompt is the dollar sign ($). The C shell (csh) was developed at Berkeley and added a number of features, such as command-line history, aliasing, built-in arithmetic, filename completion, and job control. The C shell has been favored over the Bourne shell by users running the shell interactively, but administrators prefer the Bourne shell for scripting, because Bourne shell scripts are simpler and faster than the same scripts written in C shell. The default C shell prompt is the percent sign (%). The Korn shell is a superset of the Bourne shell written by David Korn at AT&T. A number of features were added to this shell above and beyond the enhancements of the C shell. Korn shell features include an editable history, aliases, functions, regular expression wildcards, built-in arithmetic, job control, coprocessing, and special debugging features. The Bourne shell is almost completely upward-compatible with the Korn shell, so older Bourne shell programs will run fine in this shell. Novell extended the new version of the Korn shell (ksh93) to enable X Windows programming for their desktop, dtksh. Dtksh is a standard part of CDE, the Common Desktop Environment supported by most major UNIX system hardware vendors. The public domain version of the Korn shell (packages.debian.org/stable/shells/pdksh), called pdksh, is also available for multiple platforms, including Linux. Korn shell for Windows is found at www.wipro.com/uwin. The default Korn shell prompt is the dollar sign ($). 1.2.2 The Linux ShellsWhen you install Linux, you will have access to the GNU shells and tools, not the standard UNIX shells and tools. The GNU Bourne Again shell—Bash, the Linux default shell—is an enhanced Bourne shell, not only at the programming level, but also when used interactively, allowing the user to tailor his working environment and create shortcuts to improve efficiency. This shell is one of the most popular shells used by UNIX and Linux users today, and can be downloaded from www.gnu.org/software/bash/bash.html. The default Bash prompt is the dollar sign ($). Another prominent shell used by Linux users is the TC shell, a compatible offshoot of the UNIX C shell, but with many additional features.[2] See www.tcsh.org/MostRecentRelease. The default C shell prompt is the greater-than sign (>).
The Z shell is yet another Linux shell that incorporates a number of features from the Bourne Again shell, the TC shell, and the Korn shell. See sourceforge.net/ projects/zsh/. The Public Domain Korn shell (pdksh), a Korn shell clone, is also available, and for a fee, an AT&T Korn shell license can be obtained. The default Public Domain Korn shell prompt is a dollar sign ($). To see what shells are available under your version of Linux, look in the file /etc/shell, as shown in Example 1.1. Example 1.1.$ cat /etc/shell /bin/bash /bin/sh /bin/ash /bin/bsh /bin/tcsh /bin/csh /bin/ksh /bin/zsh EXPLANATION The /etc/shell (Linux) file contains a list of all shell programs available on your version of Linux. The most popular versions are bash (Bourne Again shell), tcsh (TC shell), and ksh (Korn shell). What Is POSIX?In order to provide software standards for different operating systems and their programs, the POSIX standard (also referred to as the Open Systems Standards) evolved, consisting of participants from the Institute of Electrical and Electronics Engineering (IEEE) and the International Organization for Standardization (ISO). Their goal was to supply standards that would promote application portability across different platforms, to provide a UNIX-like computing environment; thus, new software written on one machine would compile and run on another machine with different hardware. For example, a program written for a BSD UNIX machine would also run on Solaris, Linux, and HP-UX machines. In 1988 the first standard was adopted, called POSIX 1003.1. Its purpose was to provide a C language standard. In 1992, the POSIX group established standards for the shell and utilities to define the terms for developing portable shell scripts, called the IEEE 1003.2 POSIX shell standard and general utility programs. Although there is no strict enforcement of these standards, most UNIX vendors try to comply with the POSIX standard. The term "POSIX compliancy," when discussing shells and their general UNIX utilities, is an attempt to comply with the standards presented by the POSIX committee, when writing new utilities or adding enhancements to the existing ones. For example, the Bourne Again shell is a shell that is almost 100% compliant and gawk is a user utility that can operate in strict POSIX mode. |
< Day Day Up > |