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

📄 355-357.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0789717468//-->

<!--TITLE=Special Edition Using Linux, Fourth Edition//-->

<!--AUTHOR=Jack Tackett//-->

<!--AUTHOR=Jr.//-->

<!--AUTHOR=Steve Burnett//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Que//-->

<!--CHAPTER=18//-->

<!--PAGES=355-357//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="353-355.html">Previous</A></TD>

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

<TD><A HREF="357-359.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Redirecting Input and Output</FONT></H4>

<P>Many programs expect input from the terminal or keyboard; many programs send their output to the terminal screen. Linux associates keyboard input with a file named stdin; it associates terminal output with a file named stdout. You can redirect input and output so that rather than come from or go to the terminal, it comes from a file or is sent to a file.

</P>

<P>Use the &lt; (less than) symbol to redirect input into a command or program so that it comes from a file instead of the terminal. Suppose that you want to send a file named info by e-mail to someone whose address is sarah. Rather than retype the contents of the file to the <TT>mail</TT> command, give this command to use the info file as the input (stdin) to the <TT>mail</TT> command:</P>

<!-- CODE SNIP //-->

<PRE>

mail sarah &lt; info

</PRE>

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

<P>Use the &gt; (greater than) symbol to redirect the output of a program to a file. Instead of going to the terminal screen, the output is put into a file. The command <TT>date</TT> displays the current time and date on the terminal screen. If you want to store the current time and date in a file named now, enter this command:</P>

<!-- CODE SNIP //-->

<PRE>

date &gt; now

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>CAUTION:&nbsp;&nbsp;</B><BR>If the filename on the right side of the &gt; already exists, it will be overwritten. Be careful not to destroy useful information this way.<HR></FONT>

</BLOCKQUOTE>

<P>If you want to append, or concatenate, information to an existing file, use the two-character &gt;&gt; symbol. To append the current date to a file named report, enter the following command:

</P>

<!-- CODE SNIP //-->

<PRE>

date &gt;&gt; report

</PRE>

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

<P>For a slightly more lengthy example, suppose that the file named sales consists of sales data; the first field of each line contains a customer ID code. The first command line puts the output of the <TT>date</TT> command into a file named sales_report. The second command line uses the sales file as input to the <TT>sort</TT> command and appends the output to the sales_report file. The last line sends the sales_report file to users sarah and brad by e-mail:</P>

<!-- CODE SNIP //-->

<PRE>

date  &gt;  sales_report

sort  &lt;  sales  &gt;&gt;  sales_report

mail  sarah  brad  &lt;  sales_report

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>CAUTION:&nbsp;&nbsp;</B><BR>Be careful not to redirect the same file as both input and output to a command. Most likely, you&#146;ll destroy the contents of the file.<HR></FONT>

</BLOCKQUOTE>

<P>Table 18.5 summarizes the redirection symbols used in Linux.

</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 18.5</B> Linux&#146;s Redirection Symbols

<TR>

<TH COLSPAN="3"><HR>

<TR>

<TH WIDTH="18%" ALIGN="LEFT">Symbol

<TH WIDTH="32%" ALIGN="LEFT">Meaning

<TH WIDTH="50%" ALIGN="LEFT">Example

<TR>

<TH COLSPAN="3"><HR>

<TR>

<TD>&lt;

<TD>Take input from a file

<TD><TT>mail sarah &lt; report</TT>

<TR>

<TD>&gt;

<TD>Send output to a file

<TD><TT>date &gt; now</TT>

<TR>

<TD>&gt;&gt;

<TD>Append to a file

<TD><TT>date &gt;&gt; report</TT>

<TR>

<TD COLSPAN="3"><HR>

</TABLE>

<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Substituting Shell Variables</FONT></H4>

<P>You learned about shell variable expansion earlier in this chapter when you set your <TT>PATH</TT> variable to <TT>PATH=$PATH:newpath</TT>. The shell replaced <TT>$PATH</TT> with the current values of the <TT>PATH</TT> variable. Shells are really interpreted languages, almost like BASIC; the shell variable is the primary object manipulated. Because shell variables are frequently manipulated, each shell provides methods of testing and defining the shell variables.</P>

<P>Shell variables are stored as strings. When two variables are placed together, their respective strings are concatenated. For example, if you have two variables, <TT>X=hello</TT> and <TT>Y=world</TT>, the expression <TT>$X$Y</TT> results in the string <TT>helloworld</TT>. If you give the following command, the shell parses the two parameters and the values of <TT>X</TT> and <TT>Y</TT> (the two strings <TT>hello</TT> and <TT>world</TT>) are substituted before being passed to the <TT>echo</TT> command:</P>

<!-- CODE SNIP //-->

<PRE>

echo $X $Y

</PRE>

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

<P>The <TT>echo</TT> command then prints <TT>hello world</TT>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>If you place a dozen tab characters between <TT>$X</TT> and <TT>$Y</TT>, the output results are still the same.<HR></FONT>

</BLOCKQUOTE>

<P>If the substitution can be ambiguous, the shell picks the most obvious substitution&#151;often with unpredictable results. For example, if you type <TT>echo $XY</TT>, the shell substitutes <TT>helloY</TT>. If you also had a variable <TT>XY</TT>, its value is substituted instead. To get around these ambiguities, the shell has a simple mechanism to allow you to define exactly what you mean. If you type <TT>$&#123;X&#125;Y</TT>, the shell substitutes the value of <TT>X</TT> before appending the character Y to the string.</P>

<P>The Bourne and Korn shells have a rich collection of shell-variable expansion techniques that perform various tests on the variable before making the substitution. See the man pages for <TT>sh</TT> and <TT>ksh</TT> for more details.</P>

<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Substituting Command Results</FONT></H4>

<P>After the shell performs its substitution of variables, it scans the line again for commands to be run before the command line is finally ready. <BIG>Command substitution</BIG> means that Linux substitutes the results of a command for a positional parameter. This is specified in the following way:</P>

<!-- CODE SNIP //-->

<PRE>

command-1  parameter  &#145;command-2&#146;

</PRE>

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

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="353-355.html">Previous</A></TD>

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

<TD><A HREF="357-359.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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