234-236.html

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

HTML
129
字号
<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=234-236//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="236-238.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading8"></A><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 may want to redirect it to a file so you can view it later 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:</P>

<!-- CODE SNIP //-->

<PRE>

ls &gt; directory.out

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

cat test.file | sort | uniq

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

Sample dialog

Hello there

How are you today

Hello there

I am fine

</PRE>

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

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

</P>

<!-- CODE SNIP //-->

<PRE>

Hello there

How are you today

I am fine

Sample dialog

</PRE>

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

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

<H3><A NAME="Heading10"></A><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>&#36;</TT> character. If you do not like the dollar sign as the prompt or prefer to customize the prompt, 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</P>

<!-- CODE SNIP //-->

<PRE>

PS1=&#147;! Tell me what to do&#148;

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

PS2=&#147; I need more information&#148;

</PRE>

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

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

<!-- CODE SNIP //-->

<PRE>

PS1=&#147;(LOGNAME) &#148;

PS1=&#145;(&#36;PW(D) &#146;

</PRE>

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

<P>The first example causes your prompt to be equal to your UNIX username. 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 will 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 14, &#147;Shell Programming.&#148;</P>

<H3><A NAME="Heading11"></A><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 starts, 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 <TT>Ctrl&#43;Z</TT> 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 take only 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>

<!-- CODE SNIP //-->

<PRE>

control-z

bg

</PRE>

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

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

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="236-238.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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