📄 012-015.html
字号:
<HTML>
<HEAD>
<TITLE>Linux in Plain English:Linux Structures and Commands</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=1558285423//-->
<!--TITLE=Linux in Plain English//-->
<!--AUTHOR=Patrick Volkerding//-->
<!--AUTHOR=Kevin Reichard//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=1//-->
<!--PAGES=012-015//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="008-011.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="015-016.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Standard Input/Output and Redirection</FONT></H3>
<P>The third piece in the Linux puzzle concerns linking commands and files in the form of <I>standard input and output</I> (I/O). Don’t be dismayed by this techie term: standard I/O really concerns how command lines are structured and where the results of a command should be sent.</P>
<P>Linux is like every other operating system in that it needs to know where input is going to come from and where output should be sent. Other operating systems, such as Windows and the Macintosh, make assumptions when it comes to input and output. And in some circumstances so does Linux. But in most other circumstances, you need to put some thought into where your work comes from and where it goes. The basic principles behind standard I/O can be best explained with an example.</P>
<P>The <B>cat</B> command is an amazingly versatile command. It can be used to display the contents of files, add to files, and more. It can be used as a rudimentary text editor when run on a command line by itself:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat
</PRE>
<!-- END CODE SNIP //-->
<P>The cursor will go to the next line, and then you can enter text. Because you have not specified where the text should go, Linux and the <B>cat</B> command assume that the input should go to the screen. After the input goes to the screen, it’s lost forever, as there’s no mechanism for saving the text to disk. Most Linux commands assume that standard input means input from the keyboard, and standard output means display on the terminal. Under these circumstances, <B>cat</B> can be used for improving your typing skills, but otherwise it’s of little use.</P>
<P>However, <B>cat</B>’s usefulness increases when youcombine it with filenames in a command line. Combining <B>cat</B> and a filename displays an existing file on the screen, as shown in Figure 1.6.</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/01-06.jpg',589,367 )"><IMG SRC="images/01-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-06.jpg',589,367)"><FONT COLOR="#000077"><B>Figure 1.6</B></FONT></A> Combining cat with a file.</P>
<P>Instead of using standard input from the keyboard, <B>cat</B> uses standard input from a file. The contents of the file haven’t been altered; they’ve merely been displayed on the screen.</P>
<P>You can use <B>cat</B> to store keystrokes to a file with the use of <I>redirection</I> symbols. These symbols, which are part of the core operating system, are used to alter standard I/O. You could combine <B>cat</B>—or any other Linux command, for that matter—with a redirection symbol to redirect its output to a file. In the following example, the output from <B>cat</B>, which is normally sent to the screen, is instead redirected to a file named <B>kevin.report</B>:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat > kevin.report
</PRE>
<!-- END CODE SNIP //-->
<P>The output is sent one keystroke at a time to the file <B>kevin.report</B>. Typing <B>Ctrl-D</B> stops the redirection and ends the <B>cat</B> command.</P>
<P>Redirection can be used both for input and output. The <B>cat</B> command can be told to copy a file to a new filename in the following manner:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat kevin.report > pat.report
</PRE>
<!-- END CODE SNIP //-->
<P>Here, the input is <B>kevin.report</B>, and the output is <B>pat.report</B>. Nothing about <B>kevin.report</B> is changed.</P>
<P>There is a separate redirection symbol for appending text to an existing file. Here, the contents of <B>kevin.report</B> are appended to an existing file named <B>pat.report</B>:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat kevin.report >> pat.report
</PRE>
<!-- END CODE SNIP //-->
<P>If you were to omit a filename as the input, <B>cat</B> would assume that keystrokes should be used for appending. The following command line lets you append keystrokes directly to the end of the file named <B>kevin.report</B>:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat >> kevin.report
</PRE>
<!-- END CODE SNIP //-->
<P>There are actually four redirection symbols:
</P>
<DL>
<DD><B>•</B> <B>></B> is used to send the output of a command to a file or another command. For example, <B>cat > file</B> is used to send the output of the <B>cat</B> command to <B>file</B>.
<DD><B>•</B> <B><</B> is used to send the input of a file or command to a command. For example, <B>cat < file</B> means that <B>cat</B> should use <B>file</B> as input.
<DD><B>•</B> <B>>></B> is used to append input to an existing file. For example, <B>cat >> file</B> tells Linux to append the keystrokes to an existing file named <B>file</B>.
<DD><B>•</B> <B>|</B> is the pipe symbol. It’s used when you want to run a command and then send its output to another command. For instance, <B>cat | grep</B> runs the <B>cat</B> command and sends the output to the <B>grep</B> command, which is then run. (We’ll cover pipes later in this chapter.)
</DL>
<P>When you look at these symbols, it may seem that there are a few different ways to do the same thing. Indeed, < and > are interchangeable, depending on how the command line is structured. However, both symbols are needed. Command lines that look similar can actually be dealt with quite differently by the operating system. For instance, this command line:
</P>
<!-- CODE SNIP //-->
<PRE>
$ cat pat.file
</PRE>
<!-- END CODE SNIP //-->
<P>is functionally the same as:
</P>
<!-- CODE SNIP //-->
<PRE>
$ cat < pat.file
</PRE>
<!-- END CODE SNIP //-->
<P>The two command lines are actually different, however. In the first, <B>pat.file</B> is an <I>argument</I> for the <B>cat</B> command. In the Linux world, arguments are command-line modifiers that are variables; in this instance, the argument happened to be a file. In the second example, <B>pat.file</B> is input to the <B>cat</B> command.</P>
<P>There is no limit to the complexity of command lines when it comes to redirection symbols. It’s not uncommon to see two redirection symbols used as follows, especially in a shell script:</P>
<!-- CODE SNIP //-->
<PRE>
$ cat < file1 > file2
</PRE>
<!-- END CODE SNIP //-->
<P>This tells <B>cat</B> to use input from <B>file1</B> and send the output to <B>file2</B>.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="008-011.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="015-016.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 + -