📄 ch10.htm
字号:
and Korn shells. <TT>bash</TT> has a very flexible and powerful programming interface,
as well as a user-friendly command interface.</P>
<P>Why use <TT>bash</TT> instead of <TT>sh</TT>? The biggest drawback of the Bourne
shell is the way that it handles user input. Typing commands into the Bourne shell
can often be very tedious, especially if you are using it on a regular basis and
typing in a large number of commands. <TT>bash</TT> provides several features that
make entering commands much easier.
<H4 ALIGN="CENTER"><A NAME="Heading12<FONT COLOR="#000077">Command-Line Completion</FONT></H4>
<P>Often when you enter commands into <TT>bash</TT> (or any other shell), the complete
text of the command is not necessary in order for the shell to be able to determine
what you want it to do. For example, assume that the current working directory contains
the following files and subdirectories:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">News/ bin/ games/ mail/ samplefile test/
</FONT></PRE>
<P>If you want to change directories from the current working directory to the <TT>test</TT>
subdirectory, you would enter the command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cd test
</FONT></PRE>
<P>Although this command will work, <TT>bash</TT> enables you to accomplish the same
thing in a slightly different way. Since <TT>test</TT> is the only file in the directory
that begins with the letter t, <TT>bash</TT> should be able to figure out what you
want to do after you type the letter t alone:</P>
<PRE><FONT COLOR="#0066FF">cd t
</FONT></PRE>
<P>After the letter has been typed, the only thing that you could be referring to
is the <TT>test</TT> subdirectory. To get <TT>bash</TT> to finish the command for
you, press the Tab key:</P>
<PRE><FONT COLOR="#0066FF">cd t<tab>
</FONT></PRE>
<P>When you do this, <TT>bash</TT> finishes the command for you and displays it on
the screen. The command doesn't actually execute until you press the Enter key to
verify that the command <TT>bash</TT> came up with is the command that you really
intended.</P>
<P>For short commands like this, you might not see very much value in making use
of command-line completion. Using this feature may even slow you down when typing
short commands. After you get used to using command-line completion, though, and
when the commands that you are entering get a little longer, you will wonder how
anyone lived without this feature.</P>
<P>So what happens if more than one file in the directory begins with the letter
t? It would seem that this would cause a problem if you wanted to use command-line
completion. Let's see what happens when you have the following directory contents:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">News/ bin/ mail/ samplefile test/ tools/ working/
</FONT></PRE>
<P>Now you have two files in the directory that start with the letter t. Assuming
that you still want to <TT>cd</TT> into the <TT>test</TT> subdirectory, how do you
do it using command-line completion? If you type <TT>cd t<tab></TT> as you
did before, <TT>bash</TT> will not know which subdirectory you want to change to
because the information you have given is not unique.</P>
<P>If you try to do this, <TT>bash</TT> will beep to notify you that it does not
have enough information to complete the command. After beeping, <TT>bash</TT> will
leave the command on the screen as it was entered. This enables you to enter more
information without retyping what was already typed. In this case, you only need
to enter an <TT>e</TT> and press the Tab key again. This will give <TT>bash</TT>
enough information to complete the command on the command line for you to verify:</P>
<PRE><FONT COLOR="#0066FF">cd test
</FONT></PRE>
<P>If instead you decided that you want to <TT>cd</TT> into the <TT>tools</TT> subdirectory,
you could have typed</P>
<PRE><FONT COLOR="#0066FF">cd to<tab>
</FONT></PRE>
<P>This would also give <TT>bash</TT> enough information to complete the command.</P>
<P>Whenever you press the Tab key while typing a command, <TT>bash</TT> will try
to complete the command for you. If it can't complete the command, it will fill in
as much as it can and then beep, notifying you that it needs more information. You
can then enter more characters and press the Tab key again, repeating this process
until <TT>bash</TT> returns the desired command.
<H4 ALIGN="CENTER"><A NAME="Heading13<FONT COLOR="#000077">Wildcards</FONT></H4>
<P>Another way that <TT>bash</TT> makes typing commands easier is by enabling users
to use wildcards in their commands. The <TT>bash</TT> shell supports three kinds
of wildcards: <TT>*</TT> matches any character and any number of characters.</P>
<P><TT>?</TT> matches any single character.
<BLOCKQUOTE>
<P><TT>[...]</TT> matches any single character contained within the brackets.
</BLOCKQUOTE>
<P>The <TT>*</TT> wildcard can be used in a manner similar to command-line completion.
For example, assume the current directory contains the following files:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">News/ bin/ games/ mail/ samplefile test/
</FONT></PRE>
<P>If you want to <TT>cd</TT> into the <TT>test</TT> directory, you could type <TT>cd
test</TT>, or you could use command-line completion:</P>
<PRE><FONT COLOR="#0066FF">cd t<tab>
</FONT></PRE>
<P>This causes <TT>bash</TT> to complete the command for you. Now there is a third
way to do the same thing. Because only one file begins with the letter t, you could
also change to the directory by using the <TT>*</TT> wildcard. You could enter the
following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cd t*
</FONT></PRE>
<P>The <TT>*</TT> matches any character and any number of characters, so the shell
will replace the <TT>t*</TT> with <TT>test</TT> (the only file in the directory that
matches the wildcard pattern).</P>
<P>This will work reliably only if there is one file in the directory that starts
with the letter t. If more than one file in the directory starts with the letter
t, the shell will try to replace <TT>t*</TT> with the list of filenames in the directory
that match the wildcard pattern and the <TT>cd</TT> command will <TT>cd</TT> into
the first directory in this list. This will end up being the file that comes first
alphabetically, and may or may not be the intended file.</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 the current directory contains the following files:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ch1.doc ch2.doc ch3.doc chimp config mail/ test/ tools/
</FONT></PRE>
<P>If you wanted to print all of the files that have a <TT>.doc</TT> extension, you
could do so easily by entering the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">lpr *.doc
</FONT></PRE>
<P>In this case, <TT>bash</TT> will replace <TT>*.doc</TT> with the names of all
of the files in the directory that match that wildcard pattern. After <TT>bash</TT>
performed this substitution, the command that would be processed would be:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">lpr ch1.doc ch2.doc ch3.doc
</FONT></PRE>
<P>The <TT>lpr</TT> command would be invoked with the arguments of <TT>ch1.doc</TT>,
<TT>ch2.doc</TT>, and <TT>ch3.doc</TT>.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading14<FONT COLOR="#000077"><B>NOTE: </B></FONT>Given the directory
contents used in the previous example, there are several ways to print all of the
files that have a .doc extension. All of the following commands would also work:<FONT
COLOR="#0066FF"></FONT>
</DL>
<DL>
<DD><FONT COLOR="#0066FF">lpr *doc<BR>
lpr *oc<BR>
lpr *c</FONT>
<HR>
<FONT COLOR="#0066FF"></FONT>
</DL>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<P>The <TT>?</TT> wildcard functions in an identical way to the <TT>*</TT> wildcard
except that the <TT>?</TT> wildcard only matches a single character. Using the same
directory contents shown in the previous example, the <TT>?</TT> wildcard could be
used to print all of the files with the <TT>.doc</TT> extension by entering the following
command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">lpr ch?.doc
</FONT></PRE>
<P>The <TT>[...]</TT> wildcard enables you to specify certain characters or ranges
of characters to match. To print all of the files in the example that have the <TT>.doc</TT>
extension using the <TT>[...]</TT> wildcard, you would enter one of the following
two commands:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">lpr ch[123].doc
</FONT></PRE>
<P>Using a command to specify a range of characters, you would enter<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">lpr ch[1-3].doc
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading15<FONT COLOR="#000077">Command History</FONT></H4>
<P><TT>bash</TT> also supports command history. This means that <TT>bash</TT> keeps
track of a certain number of previous commands that have been entered into the shell.
The number of commands is given by a shell variable called <TT>HISTSIZE</TT>. For
more information on <TT>HISTSIZE</TT>, see the section "<TT>bash</TT> Variables"
later in this chapter.</P>
<P><TT>bash</TT> stores the text of the previous commands in a history list. When
you log into your account, the history list is initialized from a history file. The
filename of the history file can be set using the <TT>HISTFILE</TT> <TT>bash</TT>
variable. The default filename for the history file is <TT>.bash_history</TT>. This
file is usually located in your home directory. (Notice that the file begins with
a period. This means that the file is hidden and will only appear in a directory
listing if you use the <TT>-a</TT> or <TT>-A</TT> option of the <TT>ls</TT> command.)</P>
<P>Just storing previous commands into a history file is not all that useful, so
<TT>bash</TT> provides several ways of recalling them. The simplest way of using
the history list is with the up- and down-arrow keys, which scroll through the commands
that have been previously entered.</P>
<P>Pressing the up-arrow key will cause the last command that was entered to appear
on the command line. Pressing the up-arrow key again will put the command previous
to that one on the command line, and so on. If you move up in the command buffer
past the command that you wanted, you can also move down the history list a command
at a time by pressing the down-arrow key. (This is the same process used by the DOS
<TT>doskey</TT> utility.)</P>
<P>The command displayed on the command line through the history list can be edited,
if needed. <TT>bash</TT> supports a complex set of editing capabilities that are
beyond the scope of this book, but there are simple ways of editing the command line
for small and easy changes. You can use the left and right arrow keys to move along
the command line. You can insert text at any point in the command line, and can also
delete text by using the Backspace or Delete key. Most users should find these simple
editing commands sufficient.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading16<FONT COLOR="#000077"><B>NOTE: </B></FONT>The complex set
of editing commands that <TT>bash</TT> offers are similar to the commands used in
the <TT>emacs</TT> and <TT>vi</TT> text editors.
<HR>
</DL>
<P>Another method of using the history file is to display and edit the list using
the <TT>history</TT> and <TT>fc</TT> (fix command) commands built into <TT>bash</TT>.
The <TT>history</TT> command can be invoked using two different methods. The first
method uses the command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">history [n]
</FONT></PRE>
<P>When the <TT>history</TT> command is used with no options, the entire contents
of the history list are displayed. The list that is displayed on-screen might resemble
the following sample list:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1 mkdir /usr/games/pool
2 cp XpoolTable-1.2.linux.tar.z /usr/games/pool
3 cd /usr/games/pool/
4 ls
5 gunzip XpoolTable-1.2.linux.tar.z
6 tar -xf XpoolTable-1.2.linux.tar
7 ls
8 cd Xpool
9 ls
10 xinit
11 exit
12 which zip
13 zip
14 more readme
15 vi readme
16 exit
</FONT></PRE>
<P>Using the <TT>n</TT> with the <TT>history</TT> command causes the only last n
lines in the history list to be shown. So, for example, <TT>history 5</TT> shows
only the last five commands.</P>
<P>The second method of invoking the <TT>history</TT> command is used to modify the
contents of the history file, or the history list. The command has the following
command-line syntax:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">history [-r|w|a|n] [filename]
</FONT></PRE>
<P>In this form, the <TT>-r</TT> option tells the <TT>history</TT> command to read
the contents of the history file and use them as the current history list. The <TT>-w</TT>
option will cause the <TT>history</TT> command to write the current history list
to the history file (overwriting what is currently in the file). The <TT>-a</TT>
option appends the current history list to the end of the history file. The <TT>-n</TT>
option causes the lines that are in the history file to be read into the current
history list.</P>
<P>All of the options for the second form of the <TT>history</TT> command can use
the filename option as the name of the history file. If no filename is specified,
the <TT>history</TT> command will use the value of the <TT>HISTFILE</TT> shell variable.</P>
<P>The <TT>fc</TT> command can be used in two different ways to edit the command
history. In the first way, the <TT>fc</TT> command would be entered using the command-line
syntax<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">fc [-e editor_name] [-n] [-l] [-r] [first] [last]
</FONT></PRE>
<P>where all options given in braces are optional. The -<TT>e editor_</TT>name option
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -