rhl11.htm
来自「linux的初学电子书」· HTM 代码 · 共 1,875 行 · 第 1/3 页
HTM
1,875 行
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl11.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl12.htm">
<LINK REL="Previous" HREF="rhl10.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl10.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl11.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl10.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl10.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl12.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E69" >The Public Domain Korn Shell (pdksh)</A>
<UL>
<LI>
<A HREF="#E69E175" >Command-Line Completion</A>
<LI>
<A HREF="#E69E176" >Wildcards</A>
<LI>
<A HREF="#E69E177" >Command History</A>
<LI>
<A HREF="#E69E178" >Aliases</A>
<LI>
<A HREF="#E69E179" >Input Redirection</A>
<LI>
<A HREF="#E69E180" >Output Redirection</A>
<LI>
<A HREF="#E69E181" >Pipelines</A></UL>
<LI>
<A HREF="#E68E70" >Shell Prompts</A>
<LI>
<A HREF="#E68E71" >Job Control</A>
<LI>
<A HREF="#E68E72" >Key Bindings</A>
<LI>
<A HREF="#E68E73" >Customizing Your pdksh</A>
<LI>
<A HREF="#E68E74" >pdksh Commands</A>
<LI>
<A HREF="#E68E75" >pdksh Variables</A>
<LI>
<A HREF="#E68E76" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E11"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>11</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E11"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Using </B><B>pdksh</B></FONT></CENTER></H2>
<BR>
<P>In the last chapter, you saw the Bourne Again Shell (bash) in some detail. Not everyone wants to use the bash shell, so several other shells are included with most Linux systems. One of them is pdksh, a variation on the Korn Shell.
<BR>
<P>In this chapter, we look at the pdksh shell and how it can be efficiently used. After reading this chapter, you will be familiar with the following topics:
<BR>
<UL>
<LI>Command-line completion and wildcards
<BR>
<BR>
<LI>Command history and aliases
<BR>
<BR>
<LI>Input and output redirection
<BR>
<BR>
<LI>Pipelines
<BR>
<BR>
<LI>How to change your shell prompt
<BR>
<BR>
<LI>Job control
<BR>
<BR>
<LI>Key bindings
<BR>
<BR>
</UL>
<P>We will also look at how you can customize your copy of pdksh, as well as several of the important commands and variables used by the shell.
<BR>
<BR>
<A NAME="E68E69"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Public Domain Korn Shell (</B><B>pdksh</B><B>)</B></FONT></CENTER></H3>
<BR>
<P>The Korn shell, written by David Korn, was the third mainstream shell written for UNIX. Because of this, it incorporated many of the features of the Bourne and C shells (which were the first two shells). Because of the Korn shell's popularity among UNIX
users, a version was developed for Linux called the Public Domain Korn Shell, or pdksh.
<BR>
<P>The current version of the Public Domain Korn Shell does not support all of the features that exist in the commercial version of the Korn shell. It does support most of the main features, however, and adds a few new features of its own.
<BR>
<BR>
<A NAME="E69E175"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Command-Line Completion</B></FONT></CENTER></H4>
<BR>
<P>Often, when you are entering commands at the command line, the complete text of the command is not necessary in order for pdksh to be able to determine what you want to do. Command-line completion enables you to type in a partial command, and then by
entering a key sequence, tell pdksh to try to finish the command for you.
<BR>
<P>pdksh does not default to allowing the user to perform command-line completion. You must enter a command to tell pdksh that you want to be able to use command-line completion. In order to enable command-line completion, enter one of the following
commands:
<BR>
<PRE>
<FONT COLOR="#000080">set -o emacs
set -o vi</FONT></PRE>
<P>This causes pdksh to accept command editing that is similar to emacs or vi. Choose the editor that you are most familiar with, and use the basic editor commands for command-line editing.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Most people find the emacs editing mode more user-friendly than the vi editing mode.
<BR>When using vi command-line editing, you must be in command mode when you enter any of the editing commands. You can enter command mode by pressing the Esc key. When command mode has been entered, you cannot type any characters onto the command line
until you enter edit mode. There are many ways of doing this, but the usual way is by pressing the i (insert) key.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>After the command-line completion function has been enabled, you can perform command-line completion by pressing the Esc key twice (when using emacs command-line editing), or by pressing \ (when using vi command-line editing). For example, if your
current directory contains the files
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ games/ mail/ sample.text test/</FONT></PRE>
<P>and you want to edit the file sample.text using the vi text editor, you could enter the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi sample.text</FONT></PRE>
<P>After the s is typed, the only file that you could be referring to is sample.text, because it is the only file in the current directory that begins with the letter s. To get pdksh to finish the command when you are using emacs-style command editing, you
must press the Esc key twice after you type the letter s:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi s<escape><escape></FONT></PRE>
<P>To get pdksh to finish the command when you are using vi command editing, you must press the \ key after you type the letter s:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi s\</FONT></PRE>
<P>Either of these commands causes pdksh to finish the line for you and display the result on the screen. The command does not execute until you press the Enter key. This is done to give you a chance to confirm that the command pdksh came up with is the
command that you really intended.
<BR>
<P>If the sample.text file is not the only file in the directory that begins with the letter s, pdksh completes the command as far as it can and then beeps, indicating that it needs more information to complete the command.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The keyboard equivalent of pressing the Esc key is Ctrl-[, usually written as ^[. The caret (^) is the abbreviation for the Ctrl key. Pressing Esc twice using the Ctrl-[ sequence would be written as ^[^[. You might see this convention in books or man
pages.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E176"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Wildcards</B></FONT></CENTER></H4>
<BR>
<P>The pdksh shell makes typing commands easier by enabling the user to use wildcards. pdksh supports the same wildcards that bash does:
<BR>
<UL>
<UL>
<P>* matches any character and any number of characters.
<BR>
</UL></UL>
<UL>
<UL>
<P>? matches any single character.
<BR>
</UL></UL>
<UL>
<UL>
<P>[...] matches any single character contained within the brackets.
<BR>
</UL></UL>
<P>The * wildcard can be used in a way that is similar to command-line completion. For example, if the current directory contains the files
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ games/ mail/ sample.text test/</FONT></PRE>
<P>and you want to edit the sample.text file using the vi text editor, you can perform this task by using the following wildcard:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi s*</FONT></PRE>
<P>The * matches any character (and any number of characters), so pdksh replaces s* with sample.text (the only file in the directory that matches the wildcard pattern).
<BR>
<P>This works reliably if there is only one file in the directory that starts with the letter s. If more than one file starts with the same letter, the shell tries to replace s* with the list of filenames that match the wildcard pattern, and runs vi on the
first file in this list. After you quit editing the first file, the second file in the list is loaded into vi, and so on for each file that matched the wildcard pattern. If you intended to edit more than one file, this would be fine, but if you only wanted
to edit the sample.text file, this command would not work the way you expected it to.
<BR>
<P>A more practical situation in which to use the * 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:
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ games/ mail/ sample.text temp1.out
temp2.out temp3.out test/</FONT></PRE>
<P>If you want to delete all of the files with an .out extension, you can do it by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">rm *.out</FONT></PRE>
<P>In this case, pdksh replaces *.out with the names of all of the files in the directory that match the wildcard pattern. After pdksh performs this substitution, the following command is processed:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">rm temp1.out temp2.out temp3.out</FONT></PRE>
<P>The rm command is invoked with the arguments of temp1.out, temp2.out, and temp3.out.
<BR>
<P>The ? wildcard functions in a similar way to the * wildcard, except that the ? wildcard matches only a single character. Assuming the same directory contents as in the previous example, the ? wildcard can be used to delete all of the files with the .out
extension by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">rm temp?.out</FONT></PRE>
<P>The [...] 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 .doc extension, enter one of the following two commands:
<BR>
<PRE>
<FONT COLOR="#000080">rm temp[123].out
rm temp[1-3].out</FONT></PRE>
<BR>
<A NAME="E69E177"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Command History</B></FONT></CENTER></H4>
<BR>
<P>The pdksh shell supports a command history in much the same way as bash. The pdksh shell keeps track of the last HISTSIZE commands that have been entered (HISTSIZE is a user-definable pdksh variable).
<BR>
<P>pdksh stores the text of the last HISTSIZE 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 HISTFILE pdksh variable. The default filename for the
history file is .ksh_history. This file is located in your home directory. Notice that the file begins with a ., meaning that the file is hidden, and it appears in a directory listing only if you use the -a or -A option of the ls command.
<BR>
<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 pdksh, this is done differently depending on whether you are using emacs or vi command editing.
<BR>
<P>If you are using emacs command editing, you scroll up through the history list by pressing Ctrl-p, and you scroll down through the list by pressing Ctrl-n. If you are using vi command-line editing, you scroll up through the history list by pressing
either the k or - key, and you scroll down through the history list by pressing j or +.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>When using vi command editing, you must be in command mode for the key commands to work. You enter command mode by pressing the Esc key.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The command that is on the command line can be edited. The pdksh 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 keys. 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.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The complex set of editing commands that pdksh offers is similar to the commands contained in the emacs or vi text editor (you can set either emacs or vi emulation by using the set -o emacs or set -o vi commands). If you are familiar with emacs (or
vi), these commands will be familiar to you.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Another method of using the history file is to display and edit it using fc (fix command), the built-in pdksh shell command. If you read <A HREF="rhl10.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl10.htm">Chapter 10</A>, "Using bash," you may remember that bash supported another command
called history, which allowed you to view and modify the history file. The history command was left out of the pdksh shell because all of its functionality could be provided by the fc command.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Even though the history command is not built into pdksh, the command normally still works because it is usually set up as an alias to the fc -l command. For example, the .kshrc file usually contains a line such as alias history='fc -l', which
provides behavior almost identical to the history command that is built into other shells.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The fc command is used to edit the command history. It has a number of options, as is illustrated in the following command syntax:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">fc [-e ename] [-nlr] [first] [last]</FONT></PRE>
<P>All options given in braces are optional. The -e portion of the command can be used to specify the text editor that is to be used for editing the commands in the command history. 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 fc tries to find in the history list.
<BR>
<P>The -n option is used to suppress command numbers when listing the history commands that matched the specified range. The -r option lists the matched commands in reverse order. The -l command lists the matched commands to the screen. In all cases except
for the -l option, the matching commands are loaded into a text editor.
<BR>
<P>The text editor used by fc is found by taking the value of ename if the -e option was used. If this option was not used, fc uses the editor specified by the variable FCEDIT. If this variable does not exist, fc uses the value of the EDITOR variable.
Finally, if none of these variables exists, the editor chosen is vi.
<BR>
<P>If you enter the fc command with no arguments, it loads the last command that was entered into the editor. Remember that when you exit the editor, fc attempts to execute any commands that are in the editor.
<BR>
<P>The easiest way to understand what the fc command does is to look at a few examples:
<BR>
<UL>
<UL>
<P>fc loads the last command into the default editor.
<BR>
</UL></UL>
<UL>
<UL>
<P>fc -l lists the last 16 commands that were entered.
<BR>
</UL></UL>
<UL>
<UL>
<P>fc -l 5 10 lists the commands with history numbers between 5 and 10, inclusive.
<BR>
</UL></UL>
<UL>
<UL>
<P>fc 6 loads history command number 6 into the default editor.
<BR>
</UL></UL>
<UL>
<UL>
<P>fc mo loads into the default editor the most recent command that starts with the string mo.
<BR>
</UL></UL>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?