?? _chapter 9.htm
字號:
<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 9</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 8.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 10.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 9. The C Shell</h2><ul><li> <a class="docLink" href="#ch09lev1sec1">9.1 The Interactive C Shell</a></li>
<li> <a class="docLink" href="#ch09lev1sec2">9.2 Programming with the C Shell</a></li>
<li> <a class="docLink" href="#ch09lev1sec3">C SHELL LAB EXERCISES</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch09.gif" src="ch09.gif" border="0" width="500" height="805"></p>
<h3 class="docSection1Title" id="ch09lev1sec1">9.1 The Interactive C Shell</h3>
<p class="docText">Before the C shell displays a prompt, it is preceded by a
number of processes. See <a class="docLink" href="#ch09fig01">Figure 9.1</a>.</p>
<center>
<h5 id="ch09fig01" class="docFigureTitle">Figure 9.1. System startup and the C shell.</h5>
<p class="docText">
<img alt="graphics/09fig01.gif" src="09fig01.gif" border="0" width="430" height="486"></p>
</center>
<h4 class="docSection2Title" id="ch09lev2sec1">9.1.1 Startup</h4>
<p class="docText">After the system boots, the first process to run is called
<span class="docEmphasis">init;</span> it is assigned process identification
number (PID) 1. It gets instructions from a file called
<span class="docEmphasis">inittab</span> (System V) or spawns a
<span class="docEmphasis">getty</span> process (BSD). These processes are
responsible for opening up the terminal ports, for providing a place where input
comes from (<span class="docEmphasis">stdin),</span> where standard output (<span class="docEmphasis">stdout</span>)
and error (<span class="docEmphasis">stderr</span>) go, and for putting a login
prompt on your screen. The <span class="docEmphasis">/bin/login</span> program
is then executed. The login program prompts for a password, encrypts and
verifies your password, sets up an initial working environment, and then
initiates the shell, <span class="docEmphasis">/bin/csh.</span> The C shell
looks in the user's home directory for a file called <span class="docEmphasis">.cshrc,</span>
an initialization file allowing you to customize the C shell environment you
will be working in. After executing commands in the <span class="docEmphasis">.cshrc</span>
file, commands in the <span class="docEmphasis">.login</span> file are executed.
The .<span class="docEmphasis">cshrc</span> file will be executed every time a
new C shell is started. The <span class="docEmphasis">.login</span> file is
executed only once when the user logs on, and also contains commands and
variables to initialize the user's environment. After executing commands from
those files, the percent sign prompt appears on your screen and the C shell
awaits commands.</p>
<h4 class="docSection2Title" id="ch09lev2sec2">9.1.2 The Environment</h4>
<p class="docText"><b>Initialization Files.</b> After the
<span class="docEmphasis">csh</span> program starts, it is programmed to execute
two files in the user's home directory: the <span class="docEmphasis">.cshrc</span>
file and the <span class="docEmphasis">.login</span> file. These files allow
users to initialize their own environments.</p>
<p class="docText"><span class="docEmphStrong">The .<span class="docEmphasis">cshrc</span>
File.</span> The .<span class="docEmphasis">cshrc</span> file contains C shell
variable settings and is executed every time a <span class="docEmphasis">csh</span>
subshell is started. Aliases and history are normally set here.</p>
<h5 id="ch09list01" class="docExampleTitle">Example 9.1 </h5>
<pre>(The .cshrc File)
1 if ( $?prompt ) then
2 set prompt = "\! stardust > "
3 set history = 32
4 set savehist = 5
5 set noclobber
6 set filec fignore = ( .o )
7 set cdpath = ( /home/jody/ellie/bin /usr/local/bin /usr/bin )
8 set ignoreeof
9 alias m more
alias status 'date;du -s'
alias cd 'cd \!*;set prompt = "\! <$cwd> "'
endif
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the prompt has been set (<span class="docEmphasis">$?prompt</span>),
the shell is running interactively; i.e., it is not running in a script.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The primary prompt is set to the number of the current
history event, the name <span class="docEmphasis">stardust,</span> and a >
character. This will change the % prompt, the default.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">history</span> variable
is set to <span class="docEmphasis">32</span>. This controls the number of
history events that will appear on the screen. The last 32 commands you
entered will be displayed when you type <span class="docEmphasis">history</span>
(see "<a class="docLink" href="_chapter 10.htm#ch10lev2sec6">Command
Line History</a>").</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Normally, when you log out, the history list is
cleared. The <span class="docEmphasis">savehist</span> variable allows you
to save a specified number of commands from the end of the history list.
In this example, the last 5 commands will be saved in a file in your home
directory, the <span class="docEmphasis">.history</span> file, so that
when you log in again, the shell can check to see if that file exists and
put the history lines saved at the top of the new history list.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">noclobber</span> variable
is set to protect the user from inadvertently removing files when using
redirection. For example, <span class="docEmphasis">sort myfile > myfile</span>
will destroy <span class="docEmphasis">myfile.</span> With
<span class="docEmphasis">noclobber</span> set, the message
<span class="docEmphasis">file exists</span> will appear on the screen.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">filec</span> variable is
used for filename completion so that you only need to type the first
number of significant characters in a filename, press the ESC key, and the
shell will complete the rest of the filename. By pressing ^D (Control-D)
when typing in the filename, the C shell will display a list of files that
match that string. The <span class="docEmphasis">fignore</span> variable
allows you to exclude files that you do not want affected by filename
completion. In this case, all the <span class="docEmphasis">.o</span>
filenames (object files) will not be affected by <span class="docEmphasis">
filec,</span> even though <span class="docEmphasis">filec</span> is set
(see "Filename Completion: The filec Variable" on page 355).</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">cdpath</span> variable is
assigned a list of path elements. When changing directories, if you
specify just the directory name, and that directory is not a subdirectory
directly below the current working directory, the shell will search the
<span class="docEmphasis">cdpath</span> directory entries to see if it can
find the directory in any of those locations and then will change the
directory.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">ignoreeof</span> variable
prevents you from logging out with ^D. UNIX utilities that accept input
from the keyboard, such as the <span class="docEmphasis">mail</span>
program, are terminated by pressing ^D. Often, on a slow system, the user
will be tempted to press ^D more than once. The first time, the
<span class="docEmphasis">mail</span> program would be terminated, the
second time, the user is logged out. By setting <span class="docEmphasis">
ignoreeof,</span> you are required to type <span class="docEmphasis">
logout</span> to log out.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">aliases</span> are set to
give a shorthand notation for a single command or group of commands. Now
when you type the alias, the command(s) assigned to it will be executed.
The alias for the <span class="docEmphasis">more</span> command is
<span class="docEmphasis">m.</span> Every time you type
<span class="docEmphasis">m,</span> the <span class="docEmphasis">more</span>
command is executed. The <span class="docEmphasis">status</span> alias
prints the date and a summary of the user's disk usage. The
<span class="docEmphasis">cd</span> alias creates a new prompt every time
the user changes directory. The new prompt will contain the number of the
current history event (\<span class="docEmphasis">!*</span>) and the
current working directory, <span class="docEmphasis">$cwd</span>
surrounded by < >. (see "<a class="docLink" href="#ch09lev2sec5">Aliases</a>").</span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
.login</span> File.</span> The <span class="docEmphasis">.login</span> file is
executed one time when you first log in. It normally contains environment
variables and terminal settings. It is the file where window applications are
usually started. Since environment variables are inherited by processes spawned
from this shell and only need to be set once, and terminal settings do not have
to be reset for every process, those settings belong in the
<span class="docEmphasis">login</span> file.</p>
<h5 id="ch09list02" class="docExampleTitle">Example 9.2 </h5>
<pre>(The .login File)
1 stty -istrip
2 stty erase ^H
3 <span class="docEmphasis">#</span>
<span class="docEmphasis"># If possible start the windows system.</span>
<span class="docEmphasis"># Give a user a chance to bail out</span>
<span class="docEmphasis">#</span>
4 if ( 'tty' == "/dev/console" ) then
5 if ( $TERM == "sun" || $TERM == "AT386" ) then
6 if ( ${?OPENWINHOME} == 0 ) then
7 setenv OPENWINHOME /usr/openwin
8 endif
echo ""
9 echo -n "Starting OpenWindows in 5 seconds\
(type Control-C to interrupt)"
10 sleep 5
echo ""
11 $OPENWINHOME/bin/openwin
12 clear
13 logout
endif
endif
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">stty</span> command sets
options for the terminal. Input characters will not be stripped to seven
bits if <span class="docEmphasis">-istrip</span> is an option.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">stty</span> command sets
the Backspace key (Control-H) to erase characters.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Any line beginning with a <span class="docEmphasis">#</span>
is a comment. It is not an executable statement.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the current terminal window (<span class="docEmphasis">tty</span>)
is the console (<span class="docEmphasis">/dev/console</span>), the next
line is executed; otherwise, program control goes to the last
<span class="docEmphasis">endif.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the value of the <span class="docEmphasis">TERM</span>
variable is equal to sun or <span class="docEmphasis">AT386,</span> then
the next line is executed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the <span class="docEmphasis">OPENWINHOME</span>
environment variable has not been set (<span class="docEmphasis">$?</span>
is <span class="docEmphasis">0</span> if not set, and
<span class="docEmphasis">1</span> if set), the next line is executed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">OPENWINHOME</span>
environment variable is set to <span class="docEmphasis">/usr/openwin.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">This <span class="docEmphasis">endif</span> ends the
<span class="docEmphasis">if</span> on line 5.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The line is displayed on the screen, letting the user
know that <span class="docEmphasis">OpenWindows</span> is starting unless
Control-C is typed within the next <span class="docEmphasis">5</span>
seconds.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The program sleeps (stops execution) for
<span class="docEmphasis">5</span> seconds.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">openwin</span> program is
started. A set of terminal windows (shell and command tool windows and a
console window) will appear on the screen.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">After closing windows, the screen will be cleared.</span></li>
<li><span style="FONT-WEIGHT: normal">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -