⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _chapter 1.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 1</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 0.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 2.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>


<h2 class="docChapterTitle">Chapter 1. Introduction to UNIX Shells</h2><ul><li>&nbsp;<a class="docLink" href="#ch01lev1sec1">1.1 Definition and Function</a></li>
<li>&nbsp;<a class="docLink" href="#ch01lev1sec2">1.2 System Startup and the Login Shell</a></li>
<li>&nbsp;<a class="docLink" href="#ch01lev1sec3">1.3 Processes and the Shell</a></li>
<li>&nbsp;<a class="docLink" href="#ch01lev1sec4">1.4 The Environment and Inheritance</a></li>
<li>&nbsp;<a class="docLink" href="#ch01lev1sec5">1.5 Executing Commands from Scripts</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch01.gif" src="ch01.gif" border="0" width="500" height="353"></p>
<h3 class="docSection1Title" id="ch01lev1sec1">1.1 Definition and Function</h3>
<p class="docText">The shell is a special program used as an interface between 
the user and the heart of the UNIX operating system, a program called the
<span class="docEmphasis">kernel,</span> as shown in
<a class="docLink" href="#ch01fig01">Figure 1.1</a>. 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 out 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.</p>
<center>
<h5 id="ch01fig01" class="docFigureTitle">Figure 1.1. The kernel, the shell, and you.</h5>
<p class="docText">
<img alt="graphics/01fig01.gif" src="01fig01.gif" border="0" width="500" height="299"></p>
</center>
<p class="docText">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, you spend most of your time executing commands from 
the prompt. You use the shell interactively.</p>
<p class="docText">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 <span class="docEmphasis">script file,</span> and then executing 
the file. A shell script is much like a batch file: It is a list of UNIX 
commands 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 assumes that you have a good understanding of 
UNIX utilities and how they work. There are some utilities, such as
<span class="docEmphasis">grep, sed,</span> and <span class="docEmphasis">awk,</span> 
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.</p>
<h4 class="docSection2Title" id="ch01lev2sec1">1.1.1 The Three Major UNIX Shells</h4>
<p class="docText">The three prominent and supported shells on most UNIX systems 
are the <span class="docEmphasis">Bourne</span> shell (AT&amp;T shell), the
<span class="docEmphasis">C</span> shell (Berkeley shell), and the
<span class="docEmphasis">Korn</span> shell (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.</p>
<p class="docText">The Bourne shell is the standard UNIX shell, and is used to 
administer the system. Most of the system administration scripts, such as the
<span class="docEmphasis">rc</span> <span class="docEmphasis">start</span> and
<span class="docEmphasis">stop</span> scripts and <span class="docEmphasis">
shutdown</span> are Bourne shell scripts, and when in single user mode, this is 
the shell commonly used by the administrator when running as root. This shell 
was written at AT&amp;T and is known for being concise, compact, and fast. The 
default Bourne shell prompt is the dollar sign (<span class="docEmphasis">$</span>).</p>
<p class="docText">The C shell 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 (<span class="docEmphasis">%</span>).</p>
<p class="docText">The Korn shell is a superset of the Bourne shell written by 
David Korn at AT&amp;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 (<span class="docEmphasis">$</span>).</p>
<h4 class="docSection2Title" id="ch01lev2sec2">1.1.2 The Linux Shells</h4>
<p class="docText">Although often called &quot;Linux&quot; 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 (<span class="docEmphasis">bash)</span> and the TC shell (<span class="docEmphasis">tcsh)</span> 
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 (<span class="docEmphasis">pdksh</span>) 
a Korn shell clone, is also available, and for a fee you can get AT&amp;T's Korn 
shell, not to mention a host of other unknown smaller shells.</p>
<p class="docText">To see what shells are available under your version of Linux, 
look in the file, <span class="docEmphasis">/etc/shell.</span></p>
<p class="docText">To change to one of the shells listed in
<span class="docEmphasis">/etc/shell,</span> type the <span class="docEmphasis">
chsh</span> command and the name of the shell. For example, to change 
permanently to the TC shell, use the <span class="docEmphasis">chsh</span> 
command. At the prompt, type:</p>
<pre>chsh /bin/tcsh</pre>
<h4 class="docSection2Title" id="ch01lev2sec3">1.1.3 History of the Shell</h4>
<p class="docText">The first significant, standard UNIX shell was introduced in 
V7 (seventh edition of AT&amp;T) UNIX in late 1979, and was named after its creator, 
Stephen Bourne. The Bourne shell as a programming language is based on a 
language called Algol, and was primarily used to automate system administration 
tasks. Although popular for its simplicity and speed, it lacks many of the 
features for interactive use, such as history, aliasing, and job control. Enter
<span class="docEmphasis">bash,</span> the Bourne Again shell, which was 
developed by Brian Fox of the Free Software Foundation under the GNU copyright 
license and is the default shell for the very popular Linux operating system. It 
was intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools 
standard. <span class="docEmphasis">Bash</span> also offers a number of new 
features (both at the interactive and programming level) missing in the original 
Bourne shell (yet Bourne shell scripts will still run unmodified). It also 
incorporates the most useful features of both the C shell and Korn shell. It's 
big. The improvements over Bourne shell are: 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 <span class="docEmphasis">
let</span> command, etc.</p>
<p class="docText">The C shell, developed at the University of California at 
Berkeley in the late 1970s, was released as part of 2BSD UNIX. The shell, 
written primarily by Bill Joy, offered a number of additional features not 
provided in the standard Bourne shell. 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.</p>
<p class="docText">The TC shell is an expanded version of the C shell. Some of 
the new features are: command line editing (<span class="docEmphasis">emacs</span> 
and <span class="docEmphasis">vi</span>), 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.</p>
<p class="docText">With both the Bourne shell and the C shell available, the 
UNIX user now had a choice, and conflicts arose over which was the better shell. 
David Korn, from AT&amp;T, invented the Korn shell in the mid-1980s. It was released 
in 1986 and officially became part of the SVR4 distribution of UNIX in 1988. 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
<span class="docEmphasis">pdksh,</span> 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 (<span class="docEmphasis">zsh</span>), another Korn shell clone 
with TC shell features, written by Paul Falsted, and freely available at a 
number of Web sites.</p>
<h4 class="docSection2Title" id="ch01lev2sec4">1.1.4 Uses of the Shell</h4>
<p class="docText">One of the major functions of a shell is to interpret 
commands entered at the command line prompt when running interactively. The 
shell parses the command line, breaking it into words (called
<span class="docEmphasis">tokens</span>), separated by whitespace, which 
consists of tabs, spaces, or a newline. If the words contain special 
metacharacters, the shell evaluates them. The shell handles file I/O and 
background processing. After the command line has been processed, the shell 
searches for the command and starts its execution.</p>
<p class="docText">Another important function of the shell is to customize the 
user's environment, normally done in shell initialization files. 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.</p>
<p class="docText">The shell can also be used as an interpreted programming 
language. Shell programs, also called scripts, consist of commands listed in a 
file. The programs are created in an editor (although on-line scripting is 
permitted). They consist of UNIX commands interspersed with fundamental 
programming constructs such as variable assignment, conditional tests, and 
loops. You do not have to compile shell scripts. The shell interprets each line 
of the script as if it had been entered from the keyboard. Because the shell is 
responsible for interpreting commands, it is necessary for the user to have an 
understanding of what those commands are. See
<a class="docLink" href="Appendix A.htm">Appendix A</a> for a 
list of useful commands.</p>
<h4 class="docSection2Title" id="ch01lev2sec5">1.1.5 Responsibilities of the Shell</h4>
<p class="docText">The shell is ultimately responsible for making sure that any 
commands typed at the prompt get properly executed. Included in those 
responsibilities are:</p>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Reading input and parsing the command line.</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Evaluating special characters.</span></li>
  <li><span style="FONT-WEIGHT: normal">
  <p class="docList">Setting up pipes, redirection, and background processing.</span></li>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -