209-212.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=209-212//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="207-209.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="212-215.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading8"></A><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:</P>

<DL>

<DD><TT>*</TT> matches any character and any number of characters.

<DD><TT>?</TT> matches any single character.

<DD><TT>[&#133;]</TT> matches any single character contained within the brackets.

</DL>

<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:</P>

<!-- CODE SNIP //-->

<PRE>

News/ bin/  games/    mail/ samplefile  test/

</PRE>

<!-- END CODE SNIP //-->

<P>If you want to <TT>cd</TT> into the <TT>test</TT> directory, you can type <TT>cd test</TT> or use command-line completion:</P>

<!-- CODE SNIP //-->

<PRE>

cd t&lt;tab&gt;

</PRE>

<!-- END CODE SNIP //-->

<P>This causes <TT>bash</TT> to complete the command for you. Now, there&#146;s a third way to do the same thing. Because only one file begins with the letter <I>t</I>, you can also change to the directory by using the <TT>*</TT> wildcard. To do that enter the following command:</P>

<!-- CODE SNIP //-->

<PRE>

cd t*

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>*</TT> matches any character and any number of characters, so the shell replaces the <TT>t*</TT> with <TT>test</TT> (the only file in the directory that matches the wildcard pattern).</P>

<P>This only works reliably if there is one file in the directory that begins with the letter <I>t</I>. If more than one file in the directory begins with the letter <I>t</I>, the shell tries to replace <TT>t*</TT> with the list of filenames in the directory that match the wildcard pattern. The <TT>cd</TT> command <TT>cds</TT> into the first directory in this list, which is listed 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:</P>

<!-- CODE SNIP //-->

<PRE>

ch1.doc    ch2.doc   ch3.doc      chimp config   mail/  test/

tools/

</PRE>

<!-- END CODE SNIP //-->

<P>If you want to print all of the files that have a <TT>.doc</TT> extension, you can easily do so by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

lpr *.doc

</PRE>

<!-- END CODE SNIP //-->

<P>In this case, <TT>bash</TT> replaces <TT>*.doc</TT> with the names of all of the files in the directory that match that wildcard pattern. After <TT>bash</TT> performs this substitution, the command that is processed is:</P>

<!-- CODE SNIP //-->

<PRE>

lpr ch1.doc ch2.doc ch3.doc

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>lpr</TT> command can be invoked with the arguments of <TT>ch1.doc</TT>, <TT>ch2.doc</TT>, and <TT>ch3.doc</TT>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>Given the directory contents used in the previous example, there are several ways to print all of the files that have a .doc extension. Any of the following commands also work:<!-- CODE SNIP //-->

<PRE>

lpr *doc

lpr *oc

lpr *c

</PRE>

<!-- END CODE SNIP //-->

<HR></FONT>

</BLOCKQUOTE>

<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 can be used to print all of the files with the .doc extension by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

lpr ch?.doc

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>[&#133;]</TT> wildcard enables you to specify certain characters or ranges of characters to match. To print all of the files in the example with the <TT>.doc</TT> extension using the <TT>[&#133;]</TT> wildcard, enter one of the following two commands:</P>

<!-- CODE SNIP //-->

<PRE>

lpr ch[123].doc

</PRE>

<!-- END CODE SNIP //-->

<P>Using a command to specify a range of characters, enter:

</P>

<!-- CODE SNIP //-->

<PRE>

lpr ch[1-3].doc

</PRE>

<!-- END CODE SNIP //-->

<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Command History</FONT></H4>

<P><TT>bash</TT> also supports command history. This simply 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 &#147;<TT>bash</TT> Variables&#148; section later in this chapter.</P>

<P><TT>bash</TT> stores the text of the previous commands in a history list. When you log in to your account, the history list is initialized from a history file. The filename of the history file can be set using the <TT>HISTFILE 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 only appears 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 causes the last command that was entered to appear on the command line. Pressing the up-arrow again puts 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 want, you can also move down the history list a command at a time by pressing the down-arrow. (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 delete text by using the Backspace or Delete key. Most users will find these simple editing commands sufficient.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>The complex set of editing commands that <TT>bash</TT> offers is similar to the commands used in the <TT>emacs</TT> and <TT>vi</TT> text editors.<HR></FONT>

</BLOCKQUOTE>

<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 in to <TT>bash</TT>. The <TT>history</TT> command can be invoked using two different methods. The first method uses the command:</P>

<!-- CODE SNIP //-->

<PRE>

history [n]

</PRE>

<!-- END CODE SNIP //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="207-209.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="212-215.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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