228-231.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 133 行

HTML
133
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:pdksh</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=12//-->

<!--PAGES=228-231//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="225-228.html">Previous</A></TD>

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

<TD><A HREF="231-233.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Wildcards</FONT></H4>

<P>The <TT>pdksh</TT> shell makes entering commands easier by enabling the user to use wildcards. <TT>pdksh</TT> supports the same wildcards that <TT>bash</TT> does:</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 way that is similar to command-line completion. For example, if the current directory contains the files</P>

<!-- CODE SNIP //-->

<PRE>

News/    bin/    games/   mail/    sample.text    test/

</PRE>

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

<P>and you want to edit the <TT>sample.text</TT> file by using the <TT>vi</TT> text editor, you can perform this task by using the following wildcard:</P>

<!-- CODE SNIP //-->

<PRE>

vi s*

</PRE>

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

<P>The <TT>*</TT> matches any character (and any number of characters), so <TT>pdksh</TT> 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 begins with the letter &#147;<TT>s</TT>.&#148; 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 matches the wildcard pattern. If you intend to edit more than one file, this is fine, but if you only want to edit the <TT>sample.text</TT> file, this command will not work the way you need 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:</P>

<!-- CODE SNIP //-->

<PRE>

News/         bin/        games/       mail/      sample.text

temp1.out

temp2.out     temp3.out   test/

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

rm *.out

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

rm temp1.out temp2.out temp3.out

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

rm temp?.out

</PRE>

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

<P>The <TT>[&#133;]</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:</P>

<!-- CODE SNIP //-->

<PRE>

rm temp[123].out

rm temp[1-3].out

</PRE>

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

<H4 ALIGN="LEFT"><A NAME="Heading5"></A><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 pdksh</TT> variable. The default filename for the history file is <TT>.ksh_history</TT> which is located in your home directory. Notice that the file begins with a <TT>.</TT>, meaning that the file is hidden and 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, scroll up through the history list by pressing <TT>Ctrl&#43;p</TT> and scroll down through the list by pressing <TT>Ctrl&#43;n</TT>. If you are using <TT>vi</TT> command-line editing, scroll up through the history list by pressing either the <TT>k</TT> or <TT>-</TT> keys, and scroll down through the history list by pressing <TT>j</TT> or <TT>&#43;</TT>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>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></FONT>

</BLOCKQUOTE>

<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 key. 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.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>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 editors (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></FONT>

</BLOCKQUOTE>

<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 11, &#147;<TT>bash</TT>,&#148; you may remember that <TT>bash</TT> supports another command called <TT>history</TT>, which allows 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.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>Even though the <TT>history</TT> command is not built in to <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=&#146;fc -l&#146;</TT>, which provides behavior almost identical to the <TT>history</TT> command that is built in to other shells.<HR></FONT>

</BLOCKQUOTE>

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

<!-- CODE SNIP //-->

<PRE>

fc [-e ename] [-nlr] [first] [last]

</PRE>

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

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="225-228.html">Previous</A></TD>

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

<TD><A HREF="231-233.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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