📄 ch10.htm
字号:
is used to specify the text editor to be used for editing the commands. 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> will try 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. The <TT>-r</TT> option lists the matched commands in reverse order. The
<TT>-l</TT> command lists the matched commands to the screen. In all cases except
when the <TT>-l</TT> command option is used, the matchng commands will be loaded
into a text editor.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading17<FONT COLOR="#000077"><B>NOTE: </B></FONT>The text editor
used by <TT>fc</TT> is found by taking the value of editor<U> </U>name if the -<TT>e
editor _</TT>name option is used. If this option is not used, <TT>fc</TT> uses the
editor specified by the variable <TT>FCEDIT</TT>. If this variable does not exist,
<TT>fc</TT> will use the value of the <TT>EDITOR</TT> variable. Finally, if none
of these variables exists, the <TT>vi </TT>editor is chosen by default.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading18<FONT COLOR="#000077">Aliases</FONT></H4>
<P>Another way that <TT>bash</TT> makes life easier for you is by supporting command
aliases. Command aliases are commands that the user can specify. Alias commands are
usually abbreviations of other commands, designed to save keystrokes.</P>
<P>For example, if you are entering the following command on a regular basis, you
might be inclined to create an alias for it to save yourself some typing:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cd /usr/X11/lib/X11/fvwm/sample-configs
</FONT></PRE>
<P>Instead of typing this command every time you wanted to go to the <TT>sample-configs</TT>
directory, you could create an alias called <TT>goconfig</TT> that would cause the
longer command to be executed. To set up an alias like this you must use the <TT>bash</TT>
<TT>alias</TT> command. To create the <TT>goconfig</TT> alias, enter the following
command at the <TT>bash</TT> prompt:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">alias goconfig='cd /usr/X11/lib/X11/fvwm/sample-configs'
</FONT></PRE>
<P>Now, until you exit from <TT>bash</TT>, the <TT>goconfig</TT> command will cause
the original, longer command to be executed as if you had just typed it.</P>
<P>If you decide after you have entered an alias that you did not need it, you can
use the <TT>bash</TT> <TT>unalias</TT> command to delete the alias:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">unalias goconfig
</FONT></PRE>
<P>There are a number of useful aliases that most users find helpful. These can be
written in a file that you execute when you log in, to save you from typing them
each time. Some aliases that you might want to define are the following:
<UL>
<LI><TT>alias ll='ls -l'</TT>
<P>
<LI><TT>alias log='logout'</TT>
<P>
<LI><TT>alias ls='ls -F'</TT>
</UL>
<P>If you are a DOS user and are used to using DOS file commands, you can use the
<TT>alias</TT> command to define the following aliases so that Linux behaves like
DOS:
<UL>
<LI><TT>alias dir='ls'</TT>
<P>
<LI><TT>alias copy='cp'</TT>
<P>
<LI><TT>alias rename='mv'</TT>
<P>
<LI><TT>alias md='mkdir'</TT>
<P>
<LI><TT>alias rd='rmdir'</TT>
</UL>
<DL>
<DT><TT></TT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading19<FONT COLOR="#000077"><B>NOTE: </B></FONT>When defining aliases,
you can't include spaces on either side of the equal sign, or the shell can't properly
determine what you want to do. Quotation marks are necessary only 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 will display
all of the aliases that are already defined on-screen. The following listing illustrates
a sample output from the <TT>alias</TT> command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">alias dir='ls'
alias ll='ls -l'
alias ls='ls -F'
alias md='mkdir'
alias net='term < /dev/modem > /dev/modem 2> /dev/null&'
alias rd='rmdir'
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading20<FONT COLOR="#000077">Input Redirection</FONT></H4>
<P>Input redirection is used to change the source of input for a command. When a
command is entered in <TT>bash</TT>, the command is expecting some kind of input
in order to do its job. Some of the simpler commands must get all of the information
that they need passed to them on the command line. For example, the <TT>rm</TT> command
requires arguments on the command line. You must tell <TT>rm</TT> the files that
you want it to delete on the command line, or it will issue a prompt telling you
to enter <TT>rm -h</TT> for help.</P>
<P>Other commands require more elaborate input than a simple directory name. The
input for these commands can be found in a file. For example, the <TT>wc</TT> (word
count) command counts the number of characters, words, and lines in the input that
was given to it. If you just type <TT>wc <enter></TT> at the command line,
<TT>wc</TT> waits for you to tell it what it should be counting. There will be a
prompt on the command line asking for more information, but because the prompt is
sometimes not easily identifiable, it will not necessarily be obvious to you what
is happening. (It might actually appear as though <TT>bash</TT> has died, because
it is just sitting there. Everything that you type shows up on-screen, but nothing
else appears to be happening.)</P>
<P>What is actually occurring is that the <TT>wc</TT> command is collecting input
for itself. If you press Ctrl-D, the results of the <TT>wc</TT> command will be written
to the screen. If you enter the <TT>wc</TT> command with a filename as an argument,
as shown in the following example, <TT>wc</TT> will return the number of characters,
words, and lines that are contained in that file:</P>
<PRE><FONT COLOR="#0066FF">wc test
11 2 1
</FONT></PRE>
<P>Another way to pass the contents of the test file to <TT>wc</TT> is to redirect
the input of the <TT>wc</TT> command from the terminal to the <TT>test</TT> file.
This will result in the same output. The <TT><</TT> symbol is used by <TT>bash</TT>
to mean "redirect the input to the current command from the specified file."
So, redirecting <TT>wc</TT>'s input from the terminal to the <TT>test</TT> file can
be done by entering the following command:</P>
<PRE><FONT COLOR="#0066FF">wc < test
11 2 1
</FONT></PRE>
<P>Input redirection is not used all that often because most commands that require
input from a file have the option to specify a filename on the command line. There
are times, however, when you will come across a program that will not accept a filename
as an input parameter, and yet the input that you want to give exists in a file.
Whenever this situation occurs, you can use input redirection to get around the problem.
<H4 ALIGN="CENTER"><A NAME="Heading21<FONT COLOR="#000077">Output Redirection</FONT></H4>
<P>Output redirection is more commonly used than input redirection. Output redirection
enables you to redirect the output from a command into a file, as opposed to having
the output displayed on-screen.</P>
<P>There are many situations in which this can be useful. For example, if the output
of a command is quite large and will not fit on the screen, you might want to redirect
it to a file so that you can view it later using a text editor. There also may be
cases where you want to keep the output of a command to show to someone else, or
so you can print the results. Finally, output redirection is also useful if you want
to use the output from one command as input for another. (There is an easier way
to use the output of one command as input to a second command. This is shown in the
"Pipelines" section in this chapter.)</P>
<P>Output redirection is done in much the same way as input redirection. Instead
of using the <TT><</TT> symbol, the <TT>></TT> symbol is used.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading22<FONT COLOR="#000077"><B>NOTE: </B></FONT>The best way to
remember which symbol is input or output redirection is to think of the <TT><</TT>
as a funnel that is funneling input into the command (because the command receiving
the input will be on the left-hand side of the <TT><</TT>) and the <TT>></TT>
as a funnel that is funneling the output from the command into a file.
<HR>
</DL>
<P>As an example of output redirection, you can redirect the output of the <TT>ls</TT>
command into a file named <TT>directory.out</TT> using the following command:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ls > directory.out
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading23<FONT COLOR="#000077">Pipelines</FONT></H4>
<P>Pipelines are a way to string together a series of commands. This means that the
output from the first command in the pipeline is used as the input to the second
command in the pipeline. The output from the second command in the pipeline is used
as input to the third command in the pipeline, and so on. The output from the last
command in the pipeline is the output that you will actually see displayed on-screen
(or put in a file if output redirection was specified on the command line).</P>
<P>You can tell <TT>bash</TT> to create a pipeline by typing two or more commands
separated by the vertical bar or pipe character, <TT>|</TT>. The following example
illustrates the use of a pipeline:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cat sample.text | grep "High" | wc -l
</FONT></PRE>
<P>This pipeline would take the output from the <TT>cat</TT> command (which lists
the contents of a file) and send it into the <TT>grep</TT> command. The <TT>grep</TT>
command searches for each occurrence of the word High in its input. The <TT>grep</TT>
command's output would then consist of each line in the file that contained the word
High. This output is then sent to the <TT>wc</TT> command. The <TT>wc</TT> command
with the <TT>-l</TT> option prints the number of lines contained in its input.</P>
<P>To show the results on a real file, suppose the contents of <TT>sample.text</TT>
was<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">Things to do today:
Low: Go grocery shopping
High: Return movie
High: Clear level 3 in Alien vs. Predator
Medium: Pick up clothes from dry cleaner
</FONT></PRE>
<P>The pipeline would return the result <TT>2</TT>, indicating that you had two things
of high importance to complete today:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cat sample.text | grep "High" | wc -l
2
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading24<FONT COLOR="#000077">Prompts</FONT></H4>
<P><TT>bash</TT> has two levels of user prompt. The first level is what you see when
<TT>bash</TT> is waiting for a command to be typed. (This is what you normally see
when you are working with <TT>bash</TT>.)</P>
<P>The default first-level prompt is the <TT>$</TT> character. If you do not like
the <TT>$</TT> character as the prompt, or you would prefer to customize your prompt,
you can do so by setting the value of the <TT>PS1</TT> <TT>bash</TT> variable. For
example,<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">PS1="Please enter a command"
</FONT></PRE>
<P>sets the <TT>bash</TT> shell prompt to the specified string.</P>
<P>The second level of prompt is displayed when <TT>bash</TT> is expecting more input
from you in order to complete a command. The default for the second level prompt
is <TT>></TT>. If you want to change the second-level prompt, you can set the
value of the <TT>PS2</TT> variable, as in<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">PS2="I need more information"
</FONT></PRE>
<P>In addition to displaying static character strings in the command prompts (as
in the two preced- ing examples), you can also use some predefined special characters.
These special characters place things such as the current time into the prompt. Table
10.1 lists the most commonly used special-character codes. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 10.1. Prompt special character codes. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Character </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>\!</TT> </TD>
<TD ALIGN="LEFT">Displays the history number of this command. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>\#</TT> </TD>
<TD ALIGN="LEFT">Displays the command number of the current command. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>\$</TT> </TD>
<TD ALIGN="LEFT">Displays a <TT>$</TT> in the prompt unless the user is <TT>root</TT>. When the user
is root, it displays a <TT>#</TT>. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -