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

📄 unx12.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>UNIX Unleashed unx12.htm</TITLE>

<LINK REL="ToC" HREF="index.htm">

<LINK REL="Next" HREF="unx12s2.htm">

<LINK REL="Previous" HREF="unx11.htm"></HEAD>

<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>

<P><A HREF="unx11.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="unx12s2.htm">[To Chapter 12 Section 2]</A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P><UL>

<LI>

<A HREF="#I1">12 &#151; Korn Shell</A></LI>

<LI>

<A HREF="#I3">By John Valley</A></LI></UL>

<LI>

<A HREF="#I4">Shell Basics</A></LI>

<UL>

<LI>

<A HREF="#I5">Wildcard Expressions</A></LI>

<UL>

<LI>

<A HREF="#I6">Tilde Expansion</A></LI>

<LI>

<A HREF="#I7">Pattern Expressions</A></LI></UL>

<LI>

<A HREF="#I8">Command Substitution</A></LI>

<LI>

<A HREF="#I9">An Improved cd Command</A></LI></UL>

<LI>

<A HREF="#I10">Aliases</A></LI>

<UL>

<LI>

<A HREF="#I11">Defining Aliases</A></LI>

<LI>

<A HREF="#I12">Removing an Alias</A></LI>

<LI>

<A HREF="#I13">Writing an Alias Definition</A></LI>

<LI>

<A HREF="#I14">Using Exported Aliases</A></LI>

<LI>

<A HREF="#I15">Using Tracked Aliases</A></LI></UL>

<LI>

<A HREF="#I16">Shell Options</A></LI>

<LI>

<A HREF="#I17">Command History</A></LI>

<UL>

<LI>

<A HREF="#I18">Displaying the Command History List</A></LI>

<LI>

<A HREF="#I19">Reexecuting a Command from the History</A></LI>

<LI>

<A HREF="#I20">Accessing the History List fc</A></LI></UL>

<LI>

<A HREF="#I21">Command Editing</A></LI>

<UL>

<LI>

<A HREF="#I22">Activating Command Editing Mode</A></LI>

</UL>

<H1 ALIGN="CENTER">

<CENTER><A ID="I1" NAME="I1">

<BR>

<FONT SIZE=5><B>12 &#151; </B><A ID="I2" NAME="I2"></A><B>Korn Shell</B>

<BR></FONT></A></CENTER></H1>

<H5 ALIGN="CENTER">

<CENTER><A ID="I3" NAME="I3">

<FONT SIZE=3><B>By John Valley</B>

<BR></FONT></A></CENTER></H5>

<P>The previous chapter introduced the basics of UNIX shells and discussed the Bourne shell in particular. This chapter expands on the subject of shells by introducing the Korn shell&#151;the second of the three main shell languages available to you. The 
third major shell language&#151;the C shell&#151;is discussed in Chapter 13.

<BR></P>

<P>The Korn shell is named after its author, David G. Korn of AT&amp;T's Bell Laboratories, who wrote the first version of the program in 1986. The Korn shell is, therefore, a direct descendent of the Bourne shell. It is almost perfectly compatible with 
the Bourne shell. That is, with a few minor exceptions, any shell script written to be executed by the Bourne shell can be executed correctly by the Korn shell. The converse is, however, not true. As a general rule, Korn shell scripts cannot be processed 
correctly by the Bourne shell.

<BR></P>

<P>This upward compatibility provides a number of advantages, not the least of which is that it enables you to capitalize on your knowledge of the Bourne shell immediately. It also drastically reduces the amount of material that you need to learn in order 

to begin using the Korn shell.

<BR></P>

<P>Because the Korn shell is intended as a replacement for and an improvement on the Bourne shell, it is best discussed as a series of features added to the basic functionality of the Bourne shell. Many aspects of the shell's operation presented in Chapter 

11, &quot;The Bourne Shell,&quot; are not repeated here. Instead, this chapter summarizes the differences between the Bourne shell and the Korn shell.

<BR></P>

<P>The list of Korn shell enhancements is extensive, ranging from the profound to the picayune. The most dramatic enhancements are those that are intended to facilitate keyboard interaction with the shell, but there are also many important extensions to 
shell syntax and shell programming technique which should not escape your notice. Altogether, the enhancements can be collected into the following categories:

<BR></P>

<P><B>Command aliases:</B> Aliases enable you to abbreviate frequently used commands without resorting to shell programming, thus improving your overall keyboard productivity.

<BR></P>

<P><B>Command history:</B> Command history can be used alone or in conjunction with command editing to modify and reuse previously typed commands. It can also be used as a log of keyboard actions.

<BR></P>

<P><B>Command editing:</B> The Korn shell provides two styles of command editing that enable you to revise and correct commands as you type them. Command editing can greatly reduce the amount of time you spend retyping commands.

<BR></P>

<P><B>Directory Management:</B> The Korn shell provides extensions to the cd command, new pathname syntax, and new shell variables to facilitate switching between directories and to abbreviate long pathnames.

<BR></P>

<P><B>Arithmetic expressions:</B> The Bourne shell offered minimal arithmetic capabilities. The Korn shell offers much greater power for handling numbers, even though a hand-held calculator is still a better tool for calculations.

<BR></P>

<P><B>Syntax improvements:</B> The Korn shell offers improvements in the syntax of the if statement, the test built-in command, and the command substitution expression, which can improve the power and readability of your shell scripts.

<BR></P>

<P><B>Wildcard expressions: </B>The Korn shell provides more wildcard formats to reduce your typing workload.

<BR></P>

<P><B>Coprocessing:</B> The conventional pipe of the Bourne shell is expanded to permit more flexible programmed interaction between your shell script and the commands you invoke.

<BR></P>

<P><B>Job processing:</B> The Korn shell includes batch job monitoring features to simplify running processes in the background and to enable you to do more things simultaneously.

<BR></P>

<P><B>Privileged mode switching:</B> The Bourne shell provided no special features to capitalize on the set-uid capability of UNIX. The privileged mode of the Korn shell, on the other hand, enables you to switch the set-uid mode on and off and to develop 
procedures as shell scripts that previously required C language programming.

<BR></P>

<P>Although you haven't been introduced to the C shell yet, you'll find when you study it that many of the Korn shell features duplicate those of the C shell but with a different syntax. This is intentional. Although the C shell offers many desirable 
features, its general syntax is incompatible with the Bourne shell, making it somewhat of a square peg in a round hole in the UNIX world. The Korn shell solves this long-standing quandary in the UNIX world by offering the keyboard and shell programming 
features that people want but in a form compatible with the old, well established Bourne shell standard.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I4" NAME="I4">

<FONT SIZE=4><B>Shell Basics</B>

<BR></FONT></A></CENTER></H3>

<P>As I mentioned earlier, the Korn shell is essentially a foundation equivalent to the Bourne shell with a new layer of goodies added on top. You can use the Korn shell as a one-for-one replacement of the Bourne shell, with no special knowledge of Korn 
shell features. Korn shell extensions do not come into play until you explicitly invoke them.

<BR></P>

<P>In particular, the Korn shell is identical to the Bourne shell in the following areas:

<BR></P>

<P><B>Redirection of input and output:</B>The Bourne shell redirection operators &lt;, &lt;&lt;, &gt;, and &gt;&gt;, and the here document facility (<I>&lt;&lt;label</I>) all have identical syntax and work the same way.

<BR></P>

<P><B>Entering multiple commands on one line:</B>The semicolon (;) marks the end of a shell statement. To enter multiple commands on one line, simply end each command but the last with a semicolon.

<BR></P>

<P><B>Filename substitutions:</B>The Korn shell supports the familiar substitution characters *, ?, and [...], which when used in a word, cause the word to be replaced with all matching filenames. The Korn shell also supports additional filename matching 
patterns having the general form *(expression), and the tilde (~) abbreviation, but you need not use these extensions.

<BR></P>

<P><B>Substitution of variables:</B>The Korn shell supports the variable substition form $name, as well as all the special variable references $*, $@, $$, $-, and $?, and the parameters $0 through $9. The special form ${name}, as well as the forms 
${name[op]text} are also supported with their usual meaning. In addition, the Korn shell supports array variables ${name[index]}, special command substitutions $(...), and others. The extensions do not conflict with Bourne shell syntax, and you do not need 

to use them.

<BR></P>

<P><B>Command substitutions:</B>The Bourne shell command substitution form 'command' is fully supported in the Korn shell, with the same syntax and behavior as the Bourne shell format. The Korn shell also supports the variant syntax $(...) to simplify the 

use of command substitutions.

<BR></P>

<P><B>Escaping and quoting:</B>The Korn shell recognizes quoted strings of the form &quot;...&quot; and '...', with the same meaning and effect. A single special character can be deprived of its meaning with the backslash (\); the backslash is removed from 

the generated command line, except when it appears within single quotes. There are no extensions to the standard escaping and quoting techniques.

<BR></P>

<P><B>Extending a command over multiple lines</B>:To extend a command over multiple lines, end the line with a backslash (\). The backslash must be the last character of the line. The combination of the backslash, followed immediately by a newline 
character, is recognized and simply deleted from the command input. This is the same behavior as the Bourne shell.

<BR></P>

<P>The general philosophy of the Korn shell is to invoke extensions and special features with syntax that is not legal for the Bourne shell. As a result, any commands and shell scripts which are syntactically correct for the Bourne shell will be 
interpreted identically by the Korn shell. All Korn shell extensions use syntactic forms that do not appear in the Bourne shell language.

<BR></P>

<P>Features which are not invoked directly by commands, such as command history and command editing, are controlled instead by shell options. To use command editing, you must first issue the command set -o vi or set -o EMACS. If you don't, the Korn shell 
command line works the same as the Bourne shell. Also note that the set command follows the general philosophy: set -o is not valid in the Bourne shell and generates a syntax error.

<BR></P>

<P>The compatibility between the Bourne shell and Korn shell is nearly perfect, because it was one of the design objectives of the Korn shell that it should be able to execute system-provided shell scripts written for the Bourne shell, without the need to 

revise those scripts, or to invoke the Bourne shell to run them. This objective meant that even minor idiosyncracies of Bourne shell behavior could not be overlooked: the Korn shell design had to implement them all.

<BR></P>

<P>The upshot of all this is that the entire contents of Chapter 11, &quot;Bourne Shell,&quot; applies equally well, without restriction or caveat and in its entirety, to the Korn shell.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=3><B>Wildcard Expressions</B>

<BR></FONT></A></CENTER></H4>

<P>The Bourne shell supports a number of syntactic forms for abbreviating a command-line reference to filenames. These forms are based on the idea of embedding one or more special pattern-matching characters in a word. The word then becomes a template for 

filenames and is replaced by all the filenames that match the template. The pattern-matching characters supported by the Bourne shell are *, ?, and the bracketed expression [...].

<BR></P>

<P>These pattern-matching characters are supported by the Korn shell, as well as a tilde expansion that uses the ~ character to shorten pathnames, and the extended pattern-matching expressions *(), ?(), +(), @(), and !(). The syntax of pattern-matching 
expressions is based on the recognition of unquoted parentheses&#151;()&#151;in a word. Parentheses are special to the shell in both the Bourne and Korn shells; they must be quoted to avoid their special meaning. The Bourne shell attaches no special 
significance to a word such as here+(by|with), but it would complain about the parentheses. Thus, words containing embedded parentheses do not occur in the Bourne shell. The Korn shell, therefore, uses this syntax to extend wildcard pattern-matching 
without impairing Bourne shell compatibility.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=3><B>Tilde Expansion</B>

<BR></FONT></A></CENTER></H5>

<P>A word beginning with ~ (the tilde) is treated specially by the Korn shell. To avoid its special meaning, you must quote the tilde. Note that words containing a tilde in any position except for the first are treated normally. The tilde has special 
meaning only when it appeares as the first character of a word.

<BR></P>

<P>The four different styles of tilde expansion are

<BR></P>

<TABLE BORDER>

<CAPTION>Table caption goes here</CAPTION>

     <TR>

          <TD>~ </TD>

          <TD>Used by itself or when followed by a slash (/), the tilde is replaced by the pathname of your home directory. It is the same as writing $HOME or $HOME/.... For example,</TD>

     </TR>

⌨️ 快捷键说明

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