📄 355-357.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 < (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 < info
</PRE>
<!-- END CODE SNIP //-->
<P>Use the > (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 > now
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>If the filename on the right side of the > 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 >> symbol. To append the current date to a file named report, enter the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
date >> 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 > sales_report
sort < sales >> sales_report
mail sarah brad < sales_report
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>Be careful not to redirect the same file as both input and output to a command. Most likely, you’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’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><
<TD>Take input from a file
<TD><TT>mail sarah < report</TT>
<TR>
<TD>>
<TD>Send output to a file
<TD><TT>date > now</TT>
<TR>
<TD>>>
<TD>Append to a file
<TD><TT>date >> 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: </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—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>${X}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 ‘command-2’
</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 + -