212-215.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 147 行
HTML
147 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:bash</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=11//-->
<!--PAGES=212-215//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="209-212.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="215-218.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<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 onscreen might resemble the following list:</P>
<!-- CODE //-->
<PRE>
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
</PRE>
<!-- END CODE //-->
<P>Using the <TT>n</TT> with the <TT>history</TT> command causes only the last <I>n</I> 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 using the <TT>history</TT> command is to modify the contents of the history file or the history list. The command has the following command-line syntax:</P>
<!-- CODE SNIP //-->
<PRE>
history [-r|w|a|n] [<I>filename</I>]
</PRE>
<!-- END CODE SNIP //-->
<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 causes 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 uses 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 is entered using the following command-line syntax:</P>
<!-- CODE SNIP //-->
<PRE>
fc [-e <I>editor_name</I>] [-n] [-l] [-r] [<I>first</I>] [<I>last</I>]
</PRE>
<!-- END CODE SNIP //-->
<P>where all options given in braces are optional. The <TT>-e</TT> <I>editor name</I> option is used to specify the text editor to be used for editing the commands. The <I>first</I> and <I>last</I> options are used to select a range of commands to take out of the history list. <I>first</I> and <I>last</I> 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 matching commands are loaded into a text editor.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>The text editor used by <TT>fc</TT> is found by taking the value of <TT>editor name</TT> if the <TT>-e editor name</TT> 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> uses the value of the <TT>EDITOR</TT> variable. Finally, if none of these variables exists, the editor that is chosen is <TT>vi</TT>, by default.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Aliases</FONT></H4>
<P>Another way that <TT>bash</TT> makes life easier for you is by supporting command aliases. <I>Command aliases</I> are commands that the user can specify. <I>Alias commands</I> 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:</P>
<!-- CODE SNIP //-->
<PRE>
cd /usr/X11/lib/X11/fvwm/sample-configs
</PRE>
<!-- END CODE SNIP //-->
<P>Instead of typing this command every time you want to go to the <TT>sample-configs</TT> directory, you can create an alias called <TT>goconfig</TT>, which causes the longer command to be executed. To set up an alias like this you must use the <TT>bash alias</TT> command. To create the <TT>goconfig</TT> alias, enter the following command at the <TT>bash</TT> prompt:</P>
<!-- CODE SNIP //-->
<PRE>
alias goconfig=’cd /usr/X11/lib/X11/fvwm/sample-configs’
</PRE>
<!-- END CODE SNIP //-->
<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 do not need it, you can use the <TT>bash unalias</TT> command to delete the alias:</P>
<!-- CODE SNIP //-->
<PRE>
unalias goconfig
</PRE>
<!-- END CODE SNIP //-->
<P>There are a number of useful aliases that most users find helpful. These can be written in a file that executes when you log in to save you from typing them each time. Some aliases that you may want to define are:
</P>
<DL>
<DD><B>•</B> <TT>alias ll=’ls -l’: allows you to get a long directory listing with a two-letter “l” command</TT>
<DD><B>•</B> <TT>alias lo=’logout’: gives you a fast way to enter the logout command</TT>
<DD><B>•</B> <TT>alias ls=’ls -F’: adds slashes to all directory names to make them easier to see</TT>
</DL>
<P>If you are a DOS user and 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:</P>
<DL>
<DD><B>•</B> <TT>alias dir=’ls’</TT>
<DD><B>•</B> <TT>alias copy=’cp’</TT>
<DD><B>•</B> <TT>alias rename=’mv’</TT>
<DD><B>•</B> <TT>alias md=’mkdir’</TT>
<DD><B>•</B> <TT>alias rd=’rmdir’</TT>
</DL>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>When defining aliases, don’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></FONT>
</BLOCKQUOTE>
<P>If you enter the <TT>alias</TT> command without any arguments, it displays all of the aliases that are already defined. The following listing illustrates a sample output from the <TT>alias</TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
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’
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="209-212.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="215-218.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?