⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 4 页
字号:


when a command is entered in <TT>pdksh</TT>, 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 <TT>rm</TT> 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 <TT>rm -h</TT> for



help.</P>



<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 <TT>wc</TT>



(word count) command counts the number of characters, words, and lines in the input



that was given to it. If you enter the <TT>wc</TT> command with a filename as an



argument, <TT>wc</TT> returns the number of characters, words, and lines that are



contained in that file. An example of this is<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">wc test







11 2 1



</FONT></PRE>



<P>Another way to pass the contents of the test file to <TT>wc</TT> as input is to



change (or redirect) the input of the <TT>wc</TT> command from the terminal to the



test file. This results in the same output. The <TT>&lt;</TT> character is used by



<TT>pdksh</TT> to redirect the input to the current command from the file following



the character. So, redirecting <TT>wc</TT>'s input from the terminal to the test



file is done by entering the following command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">wc &lt; test



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">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.



<H4 ALIGN="CENTER"><A NAME="Heading16<FONT COLOR="#000077">Output Redirection</FONT></H4>



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



<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



<TT>&lt;</TT> symbol, the <TT>&gt;</TT> symbol is used.</P>



<P>To redirect the output of an <TT>ls</TT> command into a file named <TT>directory.out</TT>,



the following command is used:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">ls &gt; directory.out



</FONT></PRE>



<H4 ALIGN="CENTER"><A NAME="Heading17<FONT COLOR="#000077">Pipelines</FONT></H4>



<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 <TT>pdksh</TT> to create a pipeline by typing two or more commands



separated by the <TT>|</TT> character. The following is an example of using a <TT>pdksh</TT>



pipeline:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">cat test.file | sort | uniq



</FONT></PRE>



<P>This is a fairly common pipeline. Here, the contents of <TT>test.file</TT> (the



output from the <TT>cat</TT> command) are fed into the input of the <TT>sort</TT>



command. The <TT>sort</TT> command, without any options, sorts its input alphabetically



by the first field in the input. The sorted file is then piped into the <TT>uniq</TT>



command. The <TT>uniq</TT> command removes any duplicate lines from the input. If



<TT>test.file</TT> contains the lines<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Sample dialog



Hello there



How are you today



Hello there



I am fine



</FONT></PRE>



<P>the output from the pipeline is the following:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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 <TT>Hello there</TT> lines has been removed because of the <TT>uniq</TT>



command.



<H3 ALIGN="CENTER"><A NAME="Heading18<FONT COLOR="#000077">Shell Prompts</FONT></H3>



<P><TT>pdksh</TT> 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 <TT>$</TT> 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 <TT>PS1 pdksh</TT>



variable.</P>



<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<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>sets the shell prompt to the string <TT>! Tell me what to do</TT>. The <TT>pdksh</TT>



shell keeps track of how many commands have been entered since it was started. This



number is stored into the shell variable called <TT>!</TT>. When you include the



<TT>!</TT> in the prompt, it displays the current command number in the prompt. The



previous prompt command causes the command number followed by the string <TT>Tell



me what to do</TT> to be displayed on the command line each time <TT>pdksh</TT> is



expecting you to enter a command.</P>



<P>The second level of prompt is displayed when <TT>pdksh</TT> is expecting more



input from you in order to complete a command. The default for the second level prompt



is <TT>&gt;</TT>. If you want to change the second level prompt, you can do so by



setting the value of the <TT>PS2 pdksh</TT> variable, as in the following example:<FONT



COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>This causes the string <TT>I need more information</TT> to be displayed on the



command line whenever <TT>pdksh</TT> needs something from you to complete a command.</P>



<P><TT>pdksh</TT> does not support the advanced prompt options that <TT>bash</TT>



supports. There is not a predefined set of escape codes that you can put in a <TT>pdksh</TT>



prompt variable to display such items as the time or current working directory. You



can, however, put other <TT>pdksh</TT> variables into a prompt variable. For example,



the following two prompts are valid:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">PS1=&quot;(LOGNAME) &quot;



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">PS1='($PW(D) `



</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 <TT>PWD</TT> variable does not get assigned



to the variable only the first time it is executed. If you use double quotes, the



<TT>PWD</TT> 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 <TT>PS1</TT> variable to be



equal to the current value of the <TT>PWD</TT> variable. For more information on



using these quotes, see Chapter 13, &quot;Shell Programming.&quot;



<H3 ALIGN="CENTER"><A NAME="Heading19<FONT COLOR="#000077">Job Control</FONT></H3>



<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 <TT>pdksh</TT> 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.</P>



<P>Pressing the Ctrl-Z key sequence suspends a running process. The <TT>bg</TT> command



restarts a suspended process in the background, and the <TT>fg</TT> command restarts



a process in the foreground.</P>



<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 <TT>pdksh</TT>



to enter other commands while it completes running.</P>



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



<PRE><FONT COLOR="#0066FF">control-z



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">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 <TT>pdksh</TT>.



<H3 ALIGN="CENTER"><A NAME="Heading20<FONT COLOR="#000077">Key Bindings</FONT></H3>



<P>One useful feature that <TT>pdksh</TT> 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.</P>



<P>If, for example, you do not like the fact that you have to use the <TT>emacs</TT>



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:<FONT



COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>This feature effectively enables you to customize <TT>pdksh</TT> 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 <TT>bash</TT> (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 <TT>.kshrc</TT> file, which is the startup



file for the shell (it is read whenever the shell starts).</P>



<P>The <TT>bind</TT> commands that are needed to create these bindings are as listed



here:</P>



<PRE><FONT COLOR="#0066FF">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 <TT>pdksh</TT> supports by



typing the <TT>bind</TT> command without any arguments. <TT>abort</TT> (<TT>^G</TT>)



is used to abort another editing command. It is most commonly used to stop a history



list search.</P>



<P><TT>backward-char</TT> (<TT>^B</TT>) moves the cursor backward one character.



This command is often bound to the left arrow key.</P>



<P><TT>backward-word</TT> (<TT>^[b</TT>) moves the cursor backward to the beginning



of a word.</P>



<P><TT>beginning-of-line</TT> (<TT>^A</TT>) moves the cursor to the beginning of



the command line.</P>



<P><TT>complete</TT> (<TT>^[^[</TT>) tells <TT>pdksh</TT> to try to complete the



current command.</P>



<P><TT>copy-last-arg</TT> (<TT>^[_</TT>) causes the last word of the previous command



to be inserted at the cursor position.</P>



<P><TT>delete-char-backward</TT> (<TT>ERASE</TT>) deletes the character that is to



the left of the cursor.</P>



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



<P><TT>delete-word-backward</TT> (<TT>^[ERASE</TT>) deletes the characters to the



left of the cursor back to the first white space character that is encountered.</P>



<P><TT>delete-word-forward</TT> (<TT>^[(d</TT>) deletes the characters to the right



of the cursor up to the first character that occurs after a whitespace character.</P>



<P><TT>down-history</TT> (<TT>^N</TT>) moves down one line in the history list. This



command is often bound to the down arrow key.</P>



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



line.</P>



<P><TT>forward-char</TT> (<TT>^F</TT>) moves the cursor forward one character. This



command is often bound to the right arrow key.</P>



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



word.</P>



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



<P><TT>kill-to-eol</TT> (<TT>^K</TT>) deletes all of the characters to the right



of the cursor on the current line.</P>



<P><TT>list</TT> (<TT>^[?</TT>) causes <TT>pdksh</TT> to list all of the possible



command names or filenames that can complete the word in which the cursor is currently



contained.</P>



<P><TT>search-history</TT> (<TT>^R</TT>) searches the history list backward for the



first command that contains the inputted characters.</P>



<P><TT>transpose-chars</TT> (<TT>^T</TT>) exchanges the two characters on either



side of the cursor. If the cursor is at the end of the command line it switches the



last two characters on the line.











<BLOCKQUOTE>



	<P><TT>up-history</TT> (<TT>^P</TT>) moves up one command in the history list. This



	command is often bound to the up arrow key.







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading21<FONT COLOR="#000077">Customizing Your



pdksh</FONT></H3>



<P>Many ways of customizing <TT>pdksh</TT> have been described in this chapter. Until



now, though, the changes that you made only affected the current <TT>pdksh</TT> session.



As soon as you quit <TT>pdksh</TT>, all of the customizations that you made are lost.



There is a way of making the customizations more permanent.</P>



<P>This is done by storing all of your customizations in a <TT>pdksh</TT> initialization



file. Users can put commands into this file that they want to be executed each and



every time <TT>pdksh</TT> is started. Examples of commands that are typically found



in this file are aliases and initializations of variables (such as the prompts).</P>



<P>In order to set up your customization file, you must tell <TT>pdksh</TT> where



to look for the initialization file. This is different than with <TT>bash</TT>. The



<TT>bash</TT> shell automatically knew where to look for its customization file.



To tell <TT>pdksh</TT> where to look for the customization file, you must create



a file in your home directory called <TT>.profile</TT>. This file is read and all



of the commands in the file are executed each time you log into the system.</P>



<P>Here is a sample of the commands that you should place in your <TT>.profile</TT>

⌨️ 快捷键说明

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