1.1 定义和功能
shell 的作用:
(a) parse the command line;
(b) handle wildcards, redirection, pipes, and job
control;
(c) search for the command, and if found, execute that command.
总之shell 用于解释命令,自定义用户环境,以及一种程序设计语言
-
Reading input and parsing the command line.
-
Evaluating special characters.
-
Setting up pipes, redirection, and background
processing.
-
Handling signals.
-
Setting up programs for execution.
自定义用户环境的功能如下:
These files contain definitions for setting terminal keys and window
characteristics; setting variables that define the search path, permissions,
prompts, and the terminal type; and setting variables that are required for
specific applications such as windows, text-processing programs, and libraries
for programming languages. The Korn shell and C shell also provide further
customization with the addition of history and aliases, built-in variables set
to protect the user from clobbering files or inadvertently logging out, and to
notify the user when a job has completed.
3种主要的unix shell:
Bourne shell (AT&T shell), the
C shell (Berkeley shell), and the
Korn shell (superset of the Bourne shell).
Bourne shell:
用于管理系统,多数系统管理脚本用之书写, concise, compact, and fast.
the default Bourne shell prompt is the dollar sing($)
C shell
dded a number of features, such as command line history, aliasing, built-in
arithmetic, filename completion, and job control.
交互性良好,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 C shell is based on the C programming language, and when used as a
programming language, it shares a similar syntax. It also offers enhancements
for interactive use, such as command line history, aliases, and job control.
Because the shell was designed on a large machine and a number of additional
features were added, the C shell has a tendency to be slow on small machines and
sluggish even on large machines when compared to the Bourne shell.
Korn shell
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. The default Korn shell prompt is the dollar sign
(
$).
The Korn shell, really a superset of the Bourne shell, runs not only on UNIX
systems, but also on OS/2, VMS, and DOS. It provides upward-compatibility with
the Bourne shell, adds many of the popular features of the C shell, and is fast
and efficient. The Korn shell has gone through a number of revisions. The most
widely used version of the Korn shell is the 1988 version, although the 1993
version is gaining popularity. Linux users may find they are running the free
version of the Korn shell, called The Public Domain Korn shell, or simply
pdksh, a clone of David Korn's 1988 shell. It is free
and portable and currently work is underway to make it fully compatible with its
namesake, Korn shell, and to make it POSIX compliant. Also available is the Z
shell (
zsh), another Korn shell clone with TC
shell features, written by Paul Falsted, and freely available at a number of Web
sites.
The Linux Shells
Although often called "Linux" shells, Bash and TC shells are
freely available and can be compiled on any UNIX system; in fact, the shells are
now bundled with Solaris 8 and Sun's UNIX operating system. But when you install
Linux, you will have access to the GNU shells and tools, and not the standard
UNIX shells and tools. Although Linux supports a number of shells, the Bourne
Again shell (bash) and the TC shell (tcsh) are by far the most popular. The Z shell is
another Linux shell that incorporates a number of features from the Bourne Again
shell, the TC shell, and the Korn shell. The Public Domain Korn shell (pdksh) a Korn shell clone, is also available, and for a
fee you can get AT&T's Korn shell, not to mention a host of other unknown
smaller shells.
but Linux Shell --bash have some improvements :
command line history and editing, directory stacks, job control, functions,
aliases, arrays, integer arithmetic (in any base from 2 to 64), and Korn shell
features, such as extended metacharacters, select loops for creating menus, the
let command, etc.
The TC shell is an expanded version of the C shell. Some of the new features
are: command line editing (
emacs and
vi), scrolling the history list, advanced filename,
variable, and command completion, spelling correction, scheduling jobs,
automatic locking and logout, time stamps in the history list, etc. It's also
big.
1.2系统启动和登录的shell
The first process: init
The
init process initializes the system and then
starts another process to open terminal lines and set up the standard input
(
stdin), standard output (
stdout), and standard error (
stderr), which are all associated with the terminal.
The
/bin/login program then verifies your
identity by checking the first field in the
passwd file
登录的以下流程暂略,参考鸟哥部分.
The order of processing the command line is as follows:
-
History substitution is performed (if applicable).
-
Command line is broken up into tokens, or words.
-
History is updated (if applicable).
-
Quotes are processed.
-
Alias substitution and functions are defined (if
applicable).
-
Redirection, background, and pipes are set up.
-
Variable substitution ($user,
$name, etc.) is performed.
-
Command substitution (echo for today is
'date') is performed.
-
Filename substitution, called globbing (cat abc.??,
rm *.c, etc.) is performed.
-
Program execution.
Types of Commands
-
Aliases
-
Keywords
-
Functions (bash)
-
Built-in commands
-
Executable programs
Aliases and functions are defined within the shell's memory. Built-in commands
are internal routines in the shell, and executable programs reside on disk.