rhl11.htm

来自「linux的初学电子书」· HTM 代码 · 共 1,875 行 · 第 1/3 页

HTM
1,875
字号

<BR>

<A NAME="E69E178"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Aliases</B></FONT></CENTER></H4>

<BR>

<P>Another way pdksh makes life easier for you is by supporting command aliases. Command aliases are commands that you can specify and execute. Alias commands are usually abbreviations of other commands.

<BR>

<P>You tell pdksh to execute a Linux command whenever it encounters the alias. For example, if you have a file in your directory that holds a list of things that you must do each day, and you typically edit the file every morning to update it, you might 
find yourself entering the following command on a regular basis:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">vi things-to-do-today.txt</FONT></PRE>

<P>Because you are entering this command quite often, you might be inclined to create an alias for it to save yourself some typing. Instead of typing this command every time you want to edit the file, you can create an alias called ttd that causes the 
longer command to be executed.

<BR>

<P>To set up an alias such as this, you must use the pdksh alias command. To create the ttd alias, you enter the following command at the pdksh command prompt:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">alias ttd='vi things-to-do-today.txt'</FONT></PRE>

<P>From the time that you enter the alias command until the time you exit from pdksh, the ttd command causes the longer command to be executed. If you decide after you enter an alias that you no longer want that alias, you can use the pdksh unalias command 
to delete the alias:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">unalias ttd</FONT></PRE>

<P>After you use the unalias command to remove an alias, the alias no longer exists, and trying to execute it causes pdksh to report Command not found.

<BR>

<P>The following are some aliases that you might want to define:

<BR>

<UL>

<UL>

<P>alias ll='ls -l'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias log='logout'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias ls='ls -F'

<BR>

</UL></UL>

<P>If you are a DOS user and you prefer to use DOS file commands, you may also want to define the following aliases:

<BR>

<UL>

<UL>

<P>alias dir='ls'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias copy='cp'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias rename='mv'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias md='mkdir'

<BR>

</UL></UL>

<UL>

<UL>

<P>alias rd='rmdir'

<BR>

</UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When defining aliases, there must not be spaces on either side of the equal sign. The quotation marks are only necessary if the command within them contains spaces or other special characters.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>If you enter the alias command without any arguments, it prints all of the aliases that are already defined to the screen. There is a way to make sure that all of your alias commands get executed each time you start pdksh. This is done by using an 
initialization file, which we will discuss in the &quot;Customizing pdksh&quot; section, later in this chapter.

<BR>

<BR>

<A NAME="E69E179"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Input Redirection</B></FONT></CENTER></H4>

<BR>

<P>Input redirection is used to change the source of input for a command. Typically, when a command is entered in pdksh, the command expects 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. The rm command, for example, requires you to tell it on the command line which files you want to delete; if you do not specify any files it issues a prompt telling you to enter rm -h for help.

<BR>

<P>Other commands require more elaborate input than a simple directory name. The input for these commands is typically found in a file. For example, the wc (word count) command counts the number of characters, words, and lines in the input that was given 
to it. If you enter the wc command with a filename as an argument, wc returns the number of characters, words, and lines that are contained in that file. An example of this is

<BR>

<PRE>

<FONT COLOR="#000080">wc test

11 2 1</FONT></PRE>

<P>Another way to pass the contents of the test file to wc as input is to change (or redirect) the input of the wc command from the terminal to the test file. This results in the same output. The &lt; character is used by pdksh to redirect the input to the 
current command from the file following the character. So, redirecting wc's input from the terminal to the test file is done by entering the following command:

<BR>

<PRE>

<FONT COLOR="#000080">wc &lt; test

11 2 1</FONT></PRE>

<P>Input redirection is not used too often because most commands that require input from a file have an option to specify a filename on the command line. There are times, however, when you will come across a program that does not accept a filename as an 
input parameter, and yet the input that you want to give to the command exists in a file. Whenever this situation occurs, you can use input redirection to get around the problem.

<BR>

<BR>

<A NAME="E69E180"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Output Redirection</B></FONT></CENTER></H4>

<BR>

<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 the screen.

<BR>

<P>There are many situations in which this capability can be very useful. For example, if the output of a command is quite large and does not fit on the screen, you might want to redirect it to a file so you can later view it using a text editor. Output 
redirection is done in much the same way as input redirection. Instead of using the &lt; symbol, the &gt; symbol is used.

<BR>

<P>To redirect the output of an ls command into a file named directory.out, the following command is used:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">ls &gt; directory.out</FONT></PRE>

<BR>

<A NAME="E69E181"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Pipelines</B></FONT></CENTER></H4>

<BR>

<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. You can tell pdksh to create a pipeline by typing two or more commands separated 
by the | character. The following is an example of using a pdksh pipeline:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">cat test.file | sort | uniq</FONT></PRE>

<P>This is a fairly common pipeline. Here, the contents of test.file (the output from the cat command) are fed into the input of the sort command. The sort command, without any options, sorts its input alphabetically by the first field in the input. The 
sorted file is then piped into the uniq command. The uniq command removes any duplicate lines from the input. If test.file contains the lines

<BR>

<PRE>

<FONT COLOR="#000080">Sample dialog

Hello there

How are you today

Hello there

I am fine</FONT></PRE>

<P>the output from the pipeline is the following:

<BR>

<PRE>

<FONT COLOR="#000080">Hello there

How are you today

I am fine

Sample dialog</FONT></PRE>

<P>All of the lines in the file have been sorted by the first word in the line, and one of the Hello there lines has been removed because of the uniq command.

<BR>

<BR>

<A NAME="E68E70"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Shell Prompts</B></FONT></CENTER></H3>

<BR>

<P>pdksh has three levels of user prompts. The first level is what the user sees when the shell is waiting for a command to be typed. (This is what you normally see when you are working with the shell.) The default prompt is the $ character. If you do not 
like the dollar sign as the prompt or prefer to customize the prompt to your own requirements, you can do so by setting the value of the PS1 pdksh variable.

<BR>

<P>To set a variable, give the name and equal sign, and the string you want to set it to. Make sure you do not place any spaces on either side of the equal sign, or the shell will not interpret your command properly. For example, the line

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">PS1=&quot;! Tell me what to do&quot;</FONT></PRE>

<P>sets the shell prompt to the string ! Tell me what to do. The pdksh shell keeps track of how many commands have been entered since it was started. This number is stored into the shell variable called !. When you include the ! in the prompt, it displays 
the current command number in the prompt. The previous prompt command causes the command number followed by the string Tell me what to do to be displayed on the command line each time pdksh is expecting you to enter a command.

<BR>

<P>The second level of prompt is displayed when pdksh is expecting more input from you in order to complete a command. The default for the second level prompt is &gt;. If you want to change the second level prompt, you can do so by setting the value of the 
PS2 pdksh variable, as in the following example:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">PS2=&quot; I need more information&quot;</FONT></PRE>

<P>This causes the string I need more information to be displayed on the command line whenever pdksh needs something from you to complete a command.

<BR>

<P>pdksh does not support the advanced prompt options that bash supports. There is not a predefined set of escape codes that you can put in a pdksh prompt variable to display such items as the time or current working directory. You can, however, put other 
pdksh variables into a prompt variable. For example, the following two prompts are valid:

<BR>

<PRE>

<FONT COLOR="#000080">PS1=&quot;(LOGNAME) &quot;

PS1='($PW '</FONT></PRE>

<P>The first example causes your prompt to be equal to your UNIX user name. The second example causes your prompt to be the current working directory. The single quotes are needed here so that the value of the PWD variable does not get assigned to the 
variable only the first time it is executed. If you use double quotes, the PWD variable is evaluated only when the command is first entered. (The prompt would always be the directory name of the directory that you are in when you enter the command.) The 
single quotes cause the value of the PS1 variable to be equal to the current value of the PWD variable. For more information on using these quotes, see <A HREF="rhl13.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl13.htm">Chapter 13</A>, &quot;Shell Pro-gramming.&quot;

<BR>

<BR>

<A NAME="E68E71"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Job Control</B></FONT></CENTER></H3>

<BR>

<P>Job control is the capability to control the execution behavior of a currently running process. Specifically, you can suspend a running process and cause it to resume running at a later time. The pdksh shell keeps track of all of the processes that it 
started, and you can suspend a running process or restart a suspended one at any time during the life of that process.

<BR>

<P>Pressing the Ctrl-Z key sequence suspends a running process. The bg command restarts a suspended process in the background, and the fg command restarts a process in the foreground.

<BR>

<P>These commands are most often used when a user wants to run a command in the background but accidentally starts it in the foreground. When a command is started in the foreground, it locks the shell from any further user interaction until the command 
completes execution. This is usually not a problem, because most commands only take a few seconds to execute. If the command you are running is going to take a long time, you typically start the command in the background so that you can continue to use 
pdksh to enter other commands while it completes running.

<BR>

<P>If you start a command in the foreground that is going to take a long time, your shell may be tied up for several minutes. If you have done this and want to continue executing the command in the background, enter the following:

<BR>

<PRE>

<FONT COLOR="#000080">control-z

bg</FONT></PRE>

<P>This suspends the command and restarts it in the background. The command continues to execute, and you have the control of pdksh.

<BR>

<BR>

<A NAME="E68E72"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Key Bindings</B></FONT></CENTER></H3>

<BR>

<P>One useful feature that pdksh supports, which is lacking in the Bourne Again Shell, is key bindings. This feature enables you to change the behavior of key combinations for the purpose of command-line editing.

<BR>

<P>If, for example, you do not like the fact that you have to use the emacs key sequence Ctrl-P to move up in the history buffer, you can change the key sequence for that command to something else. The syntax for doing the key binding is the following:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">bind &lt;key sequence&gt; &lt;command&gt;</FONT></PRE>

<P>This feature effectively enables you to customize pdksh to have the exact feel that you want. One of the most commonly used key bindings is to bind the up, down, left, and right arrows to be used as they are in bash (for scrolling up and down the 
history list, and for moving left and right along the command line). This binding is typically found in your .kshrc file, which is the startup file for the shell (it is read whenever the shell starts).

<BR>

<P>The bind commands that are needed to create these bindings are as follows:

<BR>

<PRE>

<FONT COLOR="#000080">bind '^[['=prefix-2

bind '^XA'=up-history

bind &quot;^XB'=down-history

bind '^XC'=forward-char

bind '^XD'=backward-char</FONT></PRE>

<P>The following list gives some of the most useful editing commands that you can use for binding keys, along with the default binding and a description of each. You can get a listing of all of the editing commands that pdksh supports by typing the bind 
command without any arguments.

<BR>

<UL>

<UL>

<P>abort (^G) is used to abort another editing command. It is most commonly used to stop a history list search.

<BR>

</UL></UL>

<UL>

<UL>

<P>backward-char (^B) moves the cursor backward one character. This command is often bound to the left arrow key.

<BR>

</UL></UL>

<UL>

<UL>

<P>backward-word (^[b) moves the cursor backward to the beginning of a word.

<BR>

</UL></UL>

<UL>

<UL>

<P>beginning-of-line (^A) moves the cursor to the beginning of the command line.

<BR>

</UL></UL>

<UL>

<UL>

<P>complete (^[^[) tells pdksh to try to complete the current command.

<BR>

</UL></UL>

<UL>

<UL>

<P>copy-last-arg (^[_) causes the last word of the previous command to be inserted at the cursor position.

<BR>

</UL></UL>

<UL>

<UL>

<P>delete-char-backward (ERASE) deletes the character that is to the left of the cursor.

<BR>

</UL></UL>

<UL>

<UL>

<P>delete-char-forward deletes the character to the right of the cursor.

<BR>

</UL></UL>

<UL>

<UL>

<P>delete-word-backward (^[ERASE) deletes the characters to the left of the cursor back to the first white space character that is encountered.

<BR>

</UL></UL>

<UL>

<UL>

<P>delete-word-forward (^[ deletes the characters to the right of the cursor up to the first character that occurs after a whitespace character.

<BR>

</UL></UL>

<UL>

<UL>

<P>down-history (^N) moves down one line in the history list. This command is often bound to the down arrow key.

<BR>

</UL></UL>

<UL>

<UL>

<P>end-of-line (^E) moves the cursor to the end of the current line.

<BR>

</UL></UL>

<UL>

<UL>

<P>forward-char (^F) moves the cursor forward one character. This command is often bound to the right arrow key.

<BR>

</UL></UL>

<UL>

<UL>

<P>forward-word (^[F) moves the cursor forward to the end of a word.

<BR>

</UL></UL>

<UL>

<UL>

<P>kill-line (KILL) deletes the current line.

<BR>

</UL></UL>

<UL>

<UL>

<P>kill-to-eol (^K) deletes all of the characters to the right of the cursor on the current line.

<BR>

</UL></UL>

<UL>

<UL>

<P>list (^[?) causes pdksh to list all of the possible command names or filenames that can complete the word in which the cursor is currently contained.

<BR>

⌨️ 快捷键说明

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