?? _chapter 13.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 13</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 12.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="Appendix A.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 13. The Interactive TC Shell</h2><ul><li> <a class="docLink" href="#ch13lev1sec1">13.1 Introduction</a></li>
<li> <a class="docLink" href="#ch13lev1sec2">13.2 The TC Shell Environment</a></li>
<li> <a class="docLink" href="#ch13lev1sec3">13.3 Command Line Shortcuts</a></li>
<li> <a class="docLink" href="#ch13lev1sec4">13.4 Job Control</a></li>
<li> <a class="docLink" href="#ch13lev1sec5">13.5 Metacharacters</a></li>
<li> <a class="docLink" href="#ch13lev1sec6">13.6 Redirection and Pipes</a></li>
<li> <a class="docLink" href="#ch13lev1sec7">13.7 Variables</a></li>
<li> <a class="docLink" href="#ch13lev1sec8">13.8 Arrays</a></li>
<li> <a class="docLink" href="#ch13lev1sec9">13.9 Special Variables and Modifiers</a></li>
<li> <a class="docLink" href="#ch13lev1sec10">13.10 Command Substitution</a></li>
<li> <a class="docLink" href="#ch13lev1sec11">13.11 Quoting</a></li>
<li> <a class="docLink" href="#ch13lev1sec12">13.12 Built-In Commands</a></li>
<li> <a class="docLink" href="#ch13lev1sec13">TC SHELL LAB EXERCISES</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch13.gif" src="ch13.gif" border="0" width="500" height="411"></p>
<h3 class="docSection1Title" id="ch13lev1sec1">13.1 Introduction</h3>
<p class="docText">An interactive shell is one in which the standard input,
output, and errors are connected to a terminal. When using the TC<span id="ENB13-1"><a class="docLink" href="#EN13-1"><sup>[1]</sup></a></span>
shell (<span class="docEmphasis">tcsh</span>) interactively, you will type
commands at the <span class="docEmphasis">tcsh</span> prompt and wait for a
response. The TC shell is a program that starts up at login and interprets
commands. It is a public domain enhanced version of its predecessor, the
Berkeley UNIX C shell. Added features include command line editing, fancy
prompts, programmable completions (filenames, commands, and variables), spelling
correction, etc.</p>
<p class="docText">The primary <span class="docEmphasis">tcsh</span> source
distribution is at
<a class="docLink" href="http://ftp.astron.com" target="_blank">ftp.astron.com</a>,
also <a class="docLink" href="http://ftp.gw.com" target="_blank">ftp.gw.com</a>,
and <a class="docLink" href="http://ftp.primate.wisc.edu" target="_blank">
ftp.primate.wisc.edu</a>.<span id="ENB13-2"><a class="docLink" href="#EN13-2"><sup>[2]</sup></a></span>
Although <span class="docEmphasis">tcsh</span> is included in most Linux
distributions, it can be ported to a number of operating systems, including
Solaris, Windows NT, HP-UX, QNX, etc.</p>
<p class="docText">This chapter focuses on how to use the TC shell interactively
and how to set up your initial working environment. The enhancements over the
Berkeley C shell are extensive when working with the shell interactively, as you
will see in this chapter. However, when used as a programming language,
<span class="docEmphasis">tcsh</span> and <span class="docEmphasis">csh</span>
are practically identical. Please refer to
<a class="docLink" href="_chapter 10.htm">Chapter 10</a> for
script writing with <span class="docEmphasis">tcsh.</span> The only change will
be that where you see <span class="docEmphasis">/bin/csh,</span> you will use
<span class="docEmphasis">/bin/tcsh.</span></p>
<h4 class="docSection2Title" id="ch13lev2sec1">13.1.1 Versions of <span class="docEmphasis">tcsh</span></h4>
<p class="docText">To find out what version of <span class="docEmphasis">tcsh</span>
you are using, type at the shell prompt:</p>
<pre>which tcsh
</pre>
<p class="docText">To tell you in what directory <span class="docEmphasis">tcsh</span>
is installed (normally <span class="docEmphasis">/bin</span>), and to print the
version information, type the following:</p>
<pre>/directory_path/tcsh -c 'echo $version'
</pre>
<h5 id="ch13list01" class="docExampleTitle">Example 13.1 </h5>
<pre>1 <span class="docEmphStrong">which tcsh</span>
<span class="docEmphasis">/bin/tcsh</span>
2 <span class="docEmphStrong">/bin/tcsh -c</span> '<span class="docEmphStrong">echo $version</span>'
<span class="docEmphasis">tcsh 6.09.09 (Astron) 1998-08-16 (sparc-sun-solaris) options</span>
<span class="docEmphasis">8b,nls,dl,al,rh,color</span>
</pre>
<h4 class="docSection2Title" id="ch13lev2sec2">13.1.2 Startup</h4>
<p class="docText">Before the TC shell displays a prompt, it is preceded by a
number of processes. See <a class="docLink" href="#ch13fig01">Figure 13.1</a>.</p>
<center>
<h5 id="ch13fig01" class="docFigureTitle">Figure 13.1. System startup and the TC shell.</h5>
<p class="docText">
<img alt="graphics/13fig01.gif" src="13fig01.gif" border="0" width="456" height="504"></p>
</center>
<p class="docText">After the system boots, the first process to run is called
<span class="docEmphasis">init,</span> process identification number (PID) 1. It
spawns a <span class="docEmphasis">getty</span> process. These processes are
responsible for opening up the terminal ports, for providing a place where input
comes from (<span class="docEmphasis">stdin)</span> and 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. After the user types his user name, the
<span class="docEmphasis">/bin/login</span> program is 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/tcsh.</span> The TC shell looks in the
<span class="docEmphasis">/etc</span> directory for a system startup file called
<span class="docEmphasis">/etc/csh.cshrc</span> and <span class="docEmphasis">
/etc/csh.login</span> (if it exists). It then looks in the user's home directory
for a file called <span class="docEmphasis">~/.tcshrc,</span> another
initialization file used to customize the <span class="docEmphasis">tcsh</span>
environment. If that file is not found, it will look for another file that does
the same job, called <span class="docEmphasis">~/.cshrc</span> (normally invoked
when running <span class="docEmphasis">csh</span>). After executing commands in
the <span class="docEmphasis">.tcshrc</span> file (or <span class="docEmphasis">
.cshrc</span>), it will execute the history file, commonly called
<span class="docEmphasis">.history.</span> Then commands in the
<span class="docEmphasis">~/.login</span> file are executed, and finally the
<span class="docEmphasis">.cshdirs</span> file is executed. Each of these files
will be explained in "<a class="docLink" href="#ch13lev1sec2">The
TC Shell Environment</a>".<span id="ENB13-3"><a class="docLink" href="#EN13-3"><sup>[3]</sup></a></span></p>
<p class="docText">The <span class="docEmphasis">/etc/csh.cshrc</span> and
<span class="docEmphasis">~/.tcshrc</span> files will be executed every time a
new TC 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
all the startup files, the prompt (> is the default) appears on your screen and
the <span class="docEmphasis">tcsh</span> awaits commands. See
<a class="docLink" href="#ch13fig02">Figure 13.2</a>.</p>
<center>
<h5 id="ch13fig02" class="docFigureTitle">Figure 13.2. If any of these initialization files
exist, they are sourced in this sequence.</h5>
<p class="docText">
<img alt="graphics/13fig02.gif" src="13fig02.gif" border="0" width="379" height="478"></p>
</center>
<p class="docText">When logging out, the user presses Control-D or will be
automatically logged out if the <span class="docEmphasis">autologout</span>
shell variable has been set. Before logging off, the shell looks for a file
called <span class="docEmphasis">/etc/csh.logout</span> or
<span class="docEmphasis">~/.logout</span> in the home directory and if either
is located, its commands will be executed. The <span class="docEmphasis">.logout</span>
file normally contains commands to clean up temporary files, append data to
logfiles, wish the user a good day, etc.</p>
<h3 class="docSection1Title" id="ch13lev1sec2">13.2 The TC Shell Environment</h3>
<h4 class="docSection2Title" id="ch13lev2sec3">13.2.1 Initialization Files</h4>
<p class="docText">After the <span class="docEmphasis">tcsh</span> program
starts, it is programmed to execute a systemwide startup file,
<span class="docEmphasis">/etc/csh.cshrc,</span> and then two shell
initialization files in the user's home directory: the <span class="docEmphasis">
.tcshrc</span> file and then the <span class="docEmphasis">.login</span> file.
These files allow users to initialize their own environment.</p>
<h5 id="ch13list02" class="docExampleTitle">Example 13.2 </h5>
<pre># /etc/csh.cshrc
# <span class="docEmphasis">Systemwide environment and startup programs for csh users</span>
1 if ($?PATH) then
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -