📄 ch11.htm
字号:
replaces <TT>s*</TT> with <TT>sample.text</TT> (the only file in the directory that
matches the wildcard pattern).</P>
<P>This works reliably if there is only one file in the directory that starts with
the letter <TT>s</TT>. If more than one file starts with the same letter, the shell
tries to replace <TT>s*</TT> with the list of filenames that match the wildcard pattern,
and runs <TT>vi</TT> on the first file in this list. After you quit editing the first
file, the second file in the list is loaded into <TT>vi</TT>, and so on for each
file that matched the wildcard pattern. If you intended to edit more than one file,
this would be fine, but if you only wanted to edit the <TT>sample.text</TT> file,
this command would not work the way you expected it to.</P>
<P>A more practical situation in which to use the <TT>*</TT> wildcard is when you
want to execute the same command on multiple files that have similar filenames. For
example, assume that the current directory contains the following files:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">News/ bin/ games/ mail/ sample.text temp1.out
temp2.out temp3.out test/
</FONT></PRE>
<P>If you want to delete all of the files with an <TT>.out</TT> extension, you can
do it by entering the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">rm *.out
</FONT></PRE>
<P>In this case, <TT>pdksh</TT> replaces <TT>*.out</TT> with the names of all of
the files in the directory that match the wildcard pattern. After <TT>pdksh</TT>
performs this substitution, the following command is processed:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">rm temp1.out temp2.out temp3.out
</FONT></PRE>
<P>The <TT>rm</TT> command is invoked with the arguments of <TT>temp1.out</TT>, <TT>temp2.out</TT>,
and <TT>temp3.out</TT>.</P>
<P>The <TT>?</TT> wildcard functions in a similar way to the <TT>*</TT> wildcard,
except that the <TT>?</TT> wildcard matches only a single character. Assuming the
same directory contents as in the previous example, the <TT>?</TT> wildcard can be
used to delete all of the files with the <TT>.out</TT> extension by entering the
following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">rm temp?.out
</FONT></PRE>
<P>The <TT>[...]</TT> wildcard enables you to specify characters or ranges of characters
to match. To print all of the files in the previous example that have the <TT>.doc</TT>
extension, enter one of the following two commands:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">rm temp[123].out
rm temp[1-3].out
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Command History</FONT></H4>
<P>The <TT>pdksh</TT> shell supports a command history in much the same way as <TT>bash</TT>.
The <TT>pdksh</TT> shell keeps track of the last <TT>HISTSIZE</TT> commands that
have been entered (<TT>HISTSIZE</TT> is a user-definable <TT>pdksh</TT> variable).</P>
<P><TT>pdksh</TT> stores the text of the last <TT>HISTSIZE</TT> commands in a history
list. When you log in, the history list is initialized from a history file. The filename
of the history file can be set using the <TT>HISTFILE</TT> <TT>pdksh</TT> variable.
The default filename for the history file is <TT>.ksh_history</TT>. This file is
located in your home directory. Notice that the file begins with a <TT>.</TT>, meaning
that the file is hidden, and it appears in a directory listing only if you use the<TT>
-a</TT> or <TT>-A</TT> option of the <TT>ls</TT> command.</P>
<P>The shell provides several ways of accessing the history list. The simplest way
is to scroll through the commands that have been previously entered. In <TT>pdksh</TT>,
this is done differently depending on whether you are using <TT>emacs</TT> or <TT>vi</TT>
command editing.</P>
<P>If you are using <TT>emacs</TT> command editing, you scroll up through the history
list by pressing Ctrl-p, and you scroll down through the list by pressing Ctrl-n.
If you are using <TT>vi</TT> command-line editing, you scroll up through the history
list by pressing either the k or - key, and you scroll down through the history list
by pressing j or +.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading10<FONT COLOR="#000077"><B>NOTE: </B></FONT>When using <TT>vi</TT>
command editing, you must be in command mode for the key commands to work. You enter
command mode by pressing the Esc key.
<HR>
</DL>
<P>The command that is on the command line can be edited. The <TT>pdksh</TT> shell
supports a complex set of editing capabilities (most of which are beyond the scope
of this book). You can use the left and right arrow keys to move along the command
line. You can insert text at any point and delete text from the command line by using
the backspace or delete keys. Most users should find these simple editing commands
sufficient; for those who do not, there are many other more complicated ways of editing
the command line.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading11<FONT COLOR="#000077"><B>NOTE: </B></FONT>The complex set
of editing commands that <TT>pdksh</TT> offers is similar to the commands contained
in the <TT>emacs</TT> or <TT>vi</TT> text editor (you can set either <TT>emacs</TT>
or <TT>vi</TT> emulation by using the <TT>set -o emacs</TT> or <TT>set -o vi</TT>
commands). If you are familiar with <TT>emacs</TT> (or <TT>vi</TT>), these commands
will be familiar to you.
<HR>
</DL>
<P>Another method of using the history file is to display and edit it using <TT>fc</TT>
(fix command), the built-in <TT>pdksh</TT> shell command. If you read Chapter 10,
"<TT>bash</TT>," you may remember that <TT>bash</TT> supported another
command called <TT>history</TT>, which allowed you to view and modify the history
file. The <TT>history</TT> command was left out of the <TT>pdksh</TT> shell because
all of its functionality could be provided by the <TT>fc</TT> command.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading12<FONT COLOR="#000077"><B>TIP: </B></FONT>Even though the
<TT>history</TT> command is not built into <TT>pdksh</TT>, the command normally still
works because it is usually set up as an alias to the <TT>fc -l</TT> command. For
example, the <TT>.kshrc</TT> file usually contains a line such as <TT>alias history='fc
-l'</TT>, which provides behavior almost identical to the <TT>history</TT> command
that is built into other shells.
<HR>
</DL>
<P>The <TT>fc</TT> command is used to edit the command history. It has a number of
options, as is illustrated in the following command syntax:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">fc [-e ename] [-nlr] [first] [last]
</FONT></PRE>
<P>All options given in braces are optional. The <TT>-e</TT> portion of the command
can be used to specify the text editor that is to be used for editing the commands
in the command history. The first and last options are used to select a range of
commands to take out of the history list. first and last can refer either to the
number of a command in the history list, or to a string that <TT>fc</TT> tries to
find in the history list.</P>
<P>The <TT>-n</TT> option is used to suppress command numbers when listing the <TT>history</TT>
commands that matched the specified range. The <TT>-l</TT> command lists the matched
commands to the screen. The <TT>-r</TT> option lists the matched commands in reverse
order. In all cases except for the <TT>-l</TT> option, the matching commands are
loaded into a text editor.</P>
<P>The text editor used by <TT>fc</TT> is found by taking the value of ename if the
<TT>-e</TT> option was used. If this option was not used, <TT>fc</TT> uses the editor
specified by the variable <TT>FCEDIT</TT>. If this variable does not exist, <TT>fc</TT>
uses the value of the <TT>EDITOR</TT> variable. Finally, if none of these variables
exists, the editor chosen is <TT>vi</TT>.</P>
<P>If you enter the <TT>fc</TT> command with no arguments, it loads the last command
that was entered into the editor. Remember that when you exit the editor, <TT>fc</TT>
attempts to execute any commands that are in the editor.</P>
<P>The easiest way to understand what the <TT>fc</TT> command does is to look at
a few examples: <TT>fc</TT> loads the last command into the default editor.</P>
<P><TT>fc -l</TT> lists the last 16 commands that were entered.</P>
<P><TT>fc -l 5 10</TT> lists the commands with history numbers between 5 and 10,
inclusive.</P>
<P><TT>fc 6</TT> loads <TT>history</TT> command number 6 into the default editor.
<BLOCKQUOTE>
<P><TT>fc mo</TT> loads into the default editor the most recent command that starts
with the string <TT>mo</TT>.
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading13<FONT COLOR="#000077">Aliases</FONT></H4>
<P>Another way <TT>pdksh</TT> makes life easier for you is by supporting command
aliases. Command aliases are commands that you can specify and execute. Alias commands
are usually abbreviations of other commands.</P>
<P>You tell <TT>pdksh</TT> to execute a Linux command whenever it encounters the
alias. For example, if you have a file in your directory that holds a list of things
that you must do each day, and you typically edit the file every morning to update
it, you might find yourself entering the following command on a regular basis:</P>
<PRE><FONT COLOR="#0066FF">vi things-to-do-today.txt
</FONT></PRE>
<P>Because you are entering this command quite often, you might be inclined to create
an alias for it to save yourself some typing. Instead of typing this command every
time you want to edit the file, you can create an alias called <TT>ttd</TT> that
causes the longer command to be executed.</P>
<P>To set up an alias such as this, you must use the <TT>pdksh</TT> <TT>alias</TT>
command. To create the <TT>ttd</TT> alias, you enter the following command at the
<TT>pdksh</TT> command prompt:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">alias ttd='vi things-to-do-today.txt'
</FONT></PRE>
<P>From the time that you enter the <TT>alias</TT> command until the time you exit
from <TT>pdksh</TT>, the <TT>ttd</TT> command causes the longer command to be executed.
If you decide after you enter an alias that you no longer want that alias, you can
use the <TT>pdksh</TT> <TT>unalias</TT> command to delete the alias:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">unalias ttd
</FONT></PRE>
<P>After you use the <TT>unalias</TT> command to remove an alias, the alias no longer
exists, and trying to execute it causes <TT>pdksh</TT> to report <TT>Command not
found</TT>.</P>
<P>The following are some aliases that you might want to define: <TT>alias ll='ls
-l'</TT></P>
<P><TT>alias log='logout'</TT>
<BLOCKQUOTE>
<P><TT>alias ls='ls -F'</TT>
</BLOCKQUOTE>
<P>If you are a DOS user and you prefer to use DOS file commands, you may also want
to define the following aliases: <TT>alias dir='ls'</TT></P>
<P><TT>alias copy='cp'</TT></P>
<P><TT>alias rename='mv'</TT></P>
<P><TT>alias md='mkdir'</TT>
<BLOCKQUOTE>
<P><TT>alias rd='rmdir'</TT>
</BLOCKQUOTE>
<DL>
<DT><TT></TT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading14<FONT COLOR="#000077"><B>NOTE: </B></FONT>When defining aliases,
there must not be spaces on either side of the equal sign. The quotation marks are
only necessary if the command within them contains spaces or other special characters.
<HR>
</DL>
<P>If you enter the <TT>alias</TT> command without any arguments, it prints all of
the aliases that are already defined to the screen. There is a way to make sure that
all of your <TT>alias</TT> commands get executed each time you start <TT>pdksh</TT>.
This is done by using an initialization file, which we will discuss in the "Customizing
Your<TT> pdksh</TT>" section, later in this chapter.
<H4 ALIGN="CENTER"><A NAME="Heading15<FONT COLOR="#000077">Input Redirection</FONT></H4>
<P>Input redirection is used to change the source of input for a command. Typically,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -