📄 unx10.htm
字号:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx10.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx11.htm">
<LINK REL="Previous" HREF="unxpt2au.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unxpt2au.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx11.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">10</A></LI>
<UL>
<LI>
<A HREF="#I3">What Is a Shell?</A></LI>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I4">By Rick Rummel</A></LI></UL></UL>
<LI>
<A HREF="#I5">Introduction</A></LI>
<LI>
<A HREF="#I6">The Kernel and the Shell</A></LI>
<LI>
<A HREF="#I7">The Functions of a Shell</A></LI>
<UL>
<LI>
<A HREF="#I8">Command Line Interpretation</A></LI>
<LI>
<A HREF="#I9">Program Initiation</A></LI>
<LI>
<A HREF="#I10">Input-output Redirection</A></LI>
<LI>
<A HREF="#I11">Pipeline Connection</A></LI>
<LI>
<A HREF="#I12">Substitution of Filenames</A></LI>
<LI>
<A HREF="#I13">Maintenance of Variables</A></LI>
<LI>
<A HREF="#I14">Environment Control</A></LI>
<LI>
<A HREF="#I15">Shell Programming</A></LI></UL>
<LI>
<A HREF="#I16">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>10</B>
<BR></FONT></A></CENTER></H1>
<H2 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=5><B>What Is a Shell?</B>
<BR></FONT></A></CENTER></H2>
<H5 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=3><B>By Rick Rummel</B>
<BR></FONT></A></CENTER></H5>
<H3 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=4><B>Introduction</B>
<BR></FONT></A></CENTER></H3>
<P>You can do many things without having an extensive knowledge of how they actually work. For example, you can drive a car without understanding the physics of the internal combustion engine. A lack of knowledge of electronics doesn't prevent you from
enjoying music from a CD player. You can use a UNIX computer without knowing what the shell is and how it works. However, you will get a lot more out of UNIX if you do.
<BR></P>
<P>Three shells are typically available on a UNIX system: Bourne, Korn, and C shells. They are discussed in Chapters 11, 12, and 13. In this chapter, you'll learn
<BR></P>
<UL>
<LI>What a shell is
<BR>
<BR></LI>
<LI>What a shell does for you
<BR>
<BR></LI>
<LI>How a shell relates to the overall system
<BR>
<BR></LI></UL>
<H3 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=4><B>The Kernel and the Shell</B>
<BR></FONT></A></CENTER></H3>
<P>As the shell of a nut provides a protective covering for the kernel inside, a UNIX shell provides a protective outer covering. When you turn on, or "boot up," a UNIX-based computer, the program unix is loaded into the computer's main memory,
where it remains until you shut down the computer. This program, called the kernel, performs many low-level and system-level functions. The kernel is responsible for interpreting and sending basic instructions to the computer's processor. The kernel is
also responsible for running and scheduling processes and for carrying out all input and output. The kernel is the heart of a UNIX system. There is one and only one kernel.
<BR></P>
<P>As you might suspect from the critical nature of the kernel's responsibilities, the instructions to the kernel are complex and highly technical. To protect the user from the complexity of the kernel, and to protect the kernel from the shortcomings of
the user, a protective shell is built around the kernel. The user makes requests to a shell, which interprets them, and passes them on to the kernel. The remainder of this section explains how this outer layer is built.
<BR></P>
<P>Once the kernel is loaded to memory, it is ready to carry out user requests. First, though, a user must log in and make a request. For a user to log in, however, the kernel must know who the user is and how to communicate with him. To do this, the
kernel invokes two special programs, getty and login. For every user port—usually referred to as a tty—the kernel invokes the getty program. This process is called spawning. The getty program displays a login prompt and continuously monitors the
communication port for any type of input that it assumes is a user name. Figure 10.1 shows a freshly booted UNIX system with six user ports.
<BR></P>
<P>
<BR><B><A HREF="10unx01.gif">Figure 10.1. An active system with no users.</A></B>
<BR></P>
<P>When getty receives any input, it calls the login program, as shown in Figure 10.2. The login program establishes the identity of the user and validates his right to log in. The login program checks the password file. If the user fails to enter a valid
password, the port is returned to the control of a getty. If the user enters a valid password, login passes control by invoking the program name found in the user's entry in the password file. This program might be a word processor or a spreadsheet, but it
usually is a more generic program called a shell.
<BR></P>
<P>
<BR><B><A HREF="10unx02.gif">Figure 10.2. A user logs in.</A></B>
<BR></P>
<P>In the system shown in Figure 10.3, four users have logged in. Likewise, one user is in the process of logging in, and one port has no activity. Of the four active users, two are using the Bourne shell, one is using the Korn shell, and one has logged
into a spreadsheet. Each user has received a copy of the shell to service his requests, but there is only one kernel. Using a shell does not prevent a user from using a spreadsheet or another program, but those programs run under the active shell. A shell
is a program dedicated to a single user, and it provides an interface between the user and the UNIX kernel.
<BR></P>
<P>
<BR><B><A HREF="10unx03.gif">Figure 10.3. An active system.</A></B>
<BR></P>
<P>You don't have to use a shell to access UNIX. In Figure 10.3, one of the users has been given a spreadsheet instead of a shell. When this user logs in, the spreadsheet program starts. When he exits the spreadsheet, he is logged out. This technique is
useful in situations where security is a major concern, or when it is desirable to shield the user from any interface with UNIX. The drawback is that the user cannot use mail or the other UNIX utilities.
<BR></P>
<P>Because any program can be executed from the login—and a shell is simply a program—it is possible for you to write your own shell. In fact, three shells, developed independently, have become a standard part of UNIX. They are
<BR></P>
<UL>
<LI>The Bourne shell, developed by Stephen Bourne
<BR>
<BR></LI>
<LI>The Korn shell, developed by David Korn
<BR>
<BR></LI>
<LI>The C shell, developed by Bill Joy
<BR>
<BR></LI></UL>
<P>This variety of shells enables you to select the interface that best suits your needs or the one with which you are most familiar.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=4><B>The Functions of a Shell</B>
<BR></FONT></A></CENTER></H3>
<P>It doesn't matter which of the standard shells you choose, for all three have the same purpose: to provide a user interface to UNIX. To provide this interface, all three offer the same basic functions:
<BR></P>
<UL>
<LI>Command line interpretation
<BR>
<BR></LI>
<LI>Program initiation
<BR>
<BR></LI>
<LI>Input-output redirection
<BR>
<BR></LI>
<LI>Pipeline connection
<BR>
<BR></LI>
<LI>Substitution of filenames
<BR>
<BR></LI>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -