📄 _chapter 9.htm
字号:
<p class="docList">The user will be logged out.</span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><b>The Search Path.</b> The <span class="docEmphasis">path</span>
variable is used by the shell to locate commands typed at the command line. The
search is from left to right. The dot represents the current working directory.
If the command is not found in any of the directories listed in the path, or in
the present working directory, the shell sends the message
<span class="docEmphasis">Command not found</span> to standard error. It is
recommended that the path be set in the <span class="docEmphasis">.login</span>
file.<span id="ENB9-1"><a class="docLink" href="#EN9-1"><sup>[1]</sup></a></span>
The search path is set differently in the C shell than it is in the Bourne and
Korn shells. Each of the elements is separated by whitespace. </p>
<pre>set path = (/usr/bin /usr/ucb /bin /usr .)
echo $path
/usr/bin /usr/ucb /bin /usr .
</pre>
<p class="docText">The environment variable <span class="docEmphasis">PATH</span>
will display as:</p>
<pre>echo $PATH
/usr/bin:/usr/ucb:/bin:.
</pre>
<p class="docText">The C shell internally updates the environment variable for
<span class="docEmphasis">PATH</span> to maintain compatibility with other
programs (such as the Bourne or Korn shells) that may be started from this shell
and will need to use the <span class="docEmphasis">path</span> variable.</p>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
rehash</span> Command.</span> The shell builds an internal hash table consisting
of the contents of the directories listed in the search path. (If the dot is in
the search path, the files in the dot directory, the current working directory,
are not put in the hash table.) For efficiency, the shell uses the hash table to
find commands that are typed at the command line, rather than searching the path
each time. If a new command is added to one of the directories already listed in
the search path, the internal hash table must be recomputed. This is done by
typing</p>
<pre>% <span class="docEmphStrong">rehash</span>
</pre>
<p class="docText">(The % is the C shell prompt.) The hash table is also
automatically recomputed when you change your path at the prompt or start
another shell.</p>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
hashstat</span> Command.</span> The <span class="docEmphasis">hashstat</span>
command displays performance statistics to show the effectiveness of its search
for commands from the hash table. The statistics are in terms of "hits" and
"misses." If the shell finds most of its commands you use at the end of your
path, it has to work harder than if they were at the front of the path,
resulting in a higher number of misses than hits. In such cases, you can put the
most heavily hit directory toward the front of the path to improve performance.</p>
<pre>% <span class="docEmphStrong">hashstat</span>
<span class="docEmphasis">2 hits, 13 misses, 13%</span>
</pre>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
source</span> Command.</span> The <span class="docEmphasis">source</span>
command is a shell built-in command, that is, part of the shell's internal code.
It is used to execute a command or set of commands from a file. Normally, when a
command is executed, the shell forks a child process to execute the command, so
that any changes made will not affect the original shell, called the parent
shell. The <span class="docEmphasis">source</span> command causes the program to
be executed in the current shell, so that any variables set within the file will
become part of the environment of the current shell. The
<span class="docEmphasis">source</span> command is normally used to reexecute
the <span class="docEmphasis">.cshrc</span> or <span class="docEmphasis">.login</span>
if either has been modified. For example, if the <span class="docEmphasis">path</span>
is changed after logging in, type</p>
<pre>% <span class="docEmphStrong">source .login</span> or <span class="docEmphStrong">.cshrc</span>
</pre>
<p class="docText"><b>The Shell Prompts.</b> The C shell has two prompts: the
<span class="docEmphasis">primary</span> prompt, a percent sign (%), and the
<span class="docEmphasis">secondary</span> prompt, a question mark (?). The
primary prompt is the one displayed on the terminal after you have logged in; it
waits for you to type commands. The primary prompt can be reset. If you are
writing scripts at the prompt that require C shell programming constructs, for
example, decision-making or looping, the secondary prompt will appear so that
you can continue onto the next line. It will continue to appear after each
newline, until the construct has been properly terminated. The secondary prompt
cannot be reset.</p>
<p class="docText"><b>The Primary Prompt.</b> When running interactively, the
prompt waits for you to type a command and press the Enter key. If you do not
want to use the default prompt, reset it in the .<span class="docEmphasis">cshrc</span>
file and it will be set for this and all other C subshells. If you only want it
set for this login session, set it at the shell prompt.</p>
<h5 id="ch09list03" class="docExampleTitle">Example 9.3 </h5>
<pre>1 % <span class="docEmphStrong">set prompt = "$LOGNAME > "</span>
2 <span class="docEmphStrong">ellie ></span>
</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 primary prompt is assigned the user's login name,
followed by a > symbol and a space.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The new prompt is displayed.</span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><b>The Secondary Prompt.</b> The secondary prompt appears
when you are writing on-line scripts at the prompt. Whenever shell programming
constructs are entered, followed by a newline, the secondary prompt appears and
continues to appear until the construct is properly terminated. Writing scripts
correctly at the prompt takes practice. Once the command is entered and you
press Enter, you cannot back up, and the C shell history mechanism does not save
commands typed at the secondary prompt.</p>
<h5 id="ch09list04" class="docExampleTitle">Example 9.4 </h5>
<pre>1 % foreach pal (joe tom ann)
2 <span class="docEmphStrong">?</span> mail $pal < memo
3 <span class="docEmphStrong">?</span> end
4 %
</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">This is an example of on-line scripting. Because the C
shell is expecting further input after the <span class="docEmphasis">
foreach</span> loop is entered, the secondary prompt appears. The
<span class="docEmphasis">foreach</span> loop processes each word in the
parenthesized list.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The first time in the loop, <span class="docEmphasis">
joe</span> is assigned to the variable <span class="docEmphasis">pal.</span>
The user <span class="docEmphasis">joe</span> is sent the contents of
<span class="docEmphasis">memo</span> in the mail. Then next time through
the loop, <span class="docEmphasis">tom</span> is assigned to the variable
<span class="docEmphasis">pal,</span> and so on.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">end</span> statement
marks the end of the loop. When all of the items in the parenthesized list
have been processed, the loop ends and the primary prompt is displayed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The primary prompt is displayed.</span></li>
</ol>
</span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch09lev2sec3">9.1.3 The Command Line</h4>
<p class="docText">After logging in, the C shell displays its primary prompt, by
default a percent sign. The shell is your command interpreter. When the shell is
running interactively, it reads commands from the terminal and breaks the
command line into words. A command line consists of one or more words (or
tokens) separated by whitespace (blanks and/or tabs) and terminated with a
newline, generated by pressing the Enter key. The first word is the command, and
subsequent words are the command's options and/or arguments. The command may be
a UNIX executable program such as <span class="docEmphasis">ls</span> or
<span class="docEmphasis">pwd,</span> an alias, a built-in command such as
<span class="docEmphasis">cd</span> or <span class="docEmphasis">jobs,</span> or
a shell script. The command may contain special characters, called
<span class="docEmphasis">metacharacters,</span> that the shell must interpret
while parsing the command line. If the last character in the command line is a
backslash, followed by a newline, the line can be continued to the next line.<span id="ENB9-2"><a class="docLink" href="#EN9-2"><sup>[2]</sup></a></span></p>
<p class="docText"><b>The Exit Status.</b> After a command or program
terminates, it returns an <span class="docEmphasis">exit status</span> to the
parent process. The exit status is a number between 0 and 255. By convention,
when a program exits, if the status returned is zero, the command was successful
in its execution. When the exit status is nonzero, the command failed in some
way. The C shell status variable is set to the value of the exit status of the
last command that was executed. Success or failure of a program is determined by
the programmer who wrote the program.</p>
<h5 id="ch09list05" class="docExampleTitle">Example 9.5 </h5>
<pre>1 % grep "ellie" /etc/passwd
<span class="docEmphasis">ellie:GgMyBsSJavd16s:9496:40:Ellie Quigley:/home/jody/ellie</span>
2 % <span class="docEmphStrong">echo $status</span>
<span class="docEmphasis">0</span>
3 % grep "nicky" /etc/passwd
4 % <span class="docEmphStrong">echo $status</span>
<span class="docEmphasis">1</span>
5 % grep "scott" /etc/passsswd
<span class="docEmphasis">grep: /etc/passsswd: No such file or directory</span>
6 % <span class="docEmphStrong">echo $status</span>
<span class="docEmphasis">2</span>
</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">grep</span> program
searches for the pattern <span class="docEmphasis">ellie</span> in the
<span class="docEmphasis">/etc/passwd</span> file and is successful. The
line from <span class="docEmphasis">/etc/passwd</span> is displayed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The status variable is set to the exit value of the
<span class="docEmphasis">grep</span> command; <span class="docEmphasis">0</span>
indicates success.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">grep</span> program
cannot find user <span class="docEmphasis">nicky</span> in the
<span class="docEmphasis">/etc/passwd</span> file.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">grep</span> program
cannot find the pattern, so it returns an exit status of
<span class="docEmphasis">1.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">grep</span> fails because
the file <span class="docEmphasis">/etc/passsswd</span> cannot be opened.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList"><span class="docEmphasis">Grep</span> cannot find the
file, so it returns an exit status of <span class="docEmphasis">2.</span></span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><b>Command Grouping.</b> A command line can consist of
multiple commands. Each command is separated by a semicolon and the command line
is terminated with a newline.</p>
<h5 id="ch09list06" class="docExampleTitle">Example 9.6 </h5>
<pre>% <span class="docEmphStrong">ls; pwd; cal 2001</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The commands are executed from left to right until the
newline is reached.</td>
</tr>
</table>
<p class="docText">Commands may also be grouped so that all of the output is
either piped to another command or redirected to a file. The shell executes
commands in a subshell.</p>
<h5 id="ch09list07" class="docExampleTitle">Example 9.7 </h5>
<pre>1 % <span class="docEmphStrong">( ls ; pwd; cal 2001 ) > outputfile</span>
2 % <span class="docEmphStrong">pwd; ( cd / ; pwd ) ; pwd</span>
<span class="docEmphasis">/home/jody/ellie</span>
<span class="docEmphasis">/</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -