📄 unx04.htm
字号:
arguments. For example, the command
<BR></P>
<PRE>$<B> </B>find /usr/home/mike /usr/home/dave</PRE>
<P>produces a list of all the files in Mike's and Dave's directories and in your current working directory.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> You must specify at least one directory for a search. To specify the current directory for a search, use .pathname.
<BR></NOTE>
<HR ALIGN=CENTER>
<H3 ALIGN="CENTER">
<CENTER><A ID="I30" NAME="I30">
<FONT SIZE=4><B>Controlling Input and Output</B>
<BR></FONT></A></CENTER></H3>
<P>One thing common to almost all computer programs is that they accept some kind of input and produce some kind of output. UNIX commands are no different. In this section, you'll discover how you can control the source of input and the destination of
output.
<BR></P>
<P>One reason why UNIX is so flexible is that each program is automatically assigned three standard files: the standard input file, the standard output file, and the standard error file. Programmers are not restricted to using only these files. However,
programs and commands that use only the standard files permit maximum flexibility. The three standard files also can be redirected. When it is not redirected, the standard input file is the user's keyboard, and both standard output and standard error go to
the user's screen.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I31" NAME="I31">
<FONT SIZE=3><B>Output Redirection</B>
<BR></FONT></A></CENTER></H5>
<P>Two operators enable you to redirect output to a file: > and >>. The > operator either creates a new file that contains the redirected output, or overwrites an existing file with the redirected output. The >> operator appends the new
output to the end of the specified file. That is, if the file already contains data, the new output is added to the end of it.
<BR></P>
<P>To divert the standard output from your screen, use the > operator. Consider the directory used in an example at the beginning of this chapter. To redirect the output to a file named dirfile, you would use the command
<BR></P>
<PRE>$ ls >dirfile</PRE>
<P>Now you could use dirfile in another command. For example,
<BR></P>
<PRE>$ cat dirfile
21x
LINES.dat
LINES.idx
PAGES.dat
PAGES.idx
acct.pds
dirfile
marsha.pds
p11
t11
users</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Notice that the specified output file, dirfile, already appears in the listing. This is because the first thing that ls does is to open its output file.
<BR></NOTE>
<HR ALIGN=CENTER>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> When the output of ls is redirected, the default output is in a single column. This is useful if the result is to be processed by another command that looks for one filename per line.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The > operator causes a new file to be created. If you had already created a file named dirfile, it would be deleted and replaced with the new data. If you wanted to add the new data to the old dirfile, you could use the >> operator. For
example:
<BR></P>
<PRE>$ ls -x >dirfile
$ ls -x >>dirfile
$ cat dirfile
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds dirfile marsha.pds p11 t11
users
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds dirfile marsha.pds p11 t11
users</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I32" NAME="I32">
<FONT SIZE=3><B>Input File Redirection</B>
<BR></FONT></A></CENTER></H4>
<P>There are two possible sources of input for UNIX commands. Programs such as ls and find get their input from the command line in the form of options and filenames. Other programs, such as cat, can get their data from the standard input as well as from
the command line. Try the cat command with no options on the command line:
<BR></P>
<PRE>$ <B>cat</B></PRE>
<P>There is no response. Because no files are specified with the command, cat waits to get its input from your keyboard, the standard input file. The program will accept input lines from the keyboard until it sees a line which begins with Ctrl+D, which is
the end-of-file signal for standard input.
<BR></P>
<P>To redirect the standard input, you use the < operator. For example, if you wanted cat to get its input from dirfile, you could use the command
<BR></P>
<PRE>$ cat <dirfile</PRE>
<P>The difference between this command and
<BR></P>
<PRE>$ cat dirfile</PRE>
<P>is a subtle one. In filenames provided as options to a command, you can use filename substitution. When redirecting input, you must use the name of an existing file or device. Therefore, the following command is a valid UNIX command:
<BR></P>
<PRE>$ cat dir*</PRE>
<P>You cannot, however, use the following command, for it is an invalid UNIX command:
<BR></P>
<PRE>$ cat <dir*</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I33" NAME="I33">
<FONT SIZE=3><B>Redirecting Error Messages</B>
<BR></FONT></A></CENTER></H4>
<P>Most commands have two possible types of output: normal or standard output, and error messages. Normally, error messages display to the screen, but error messages can also be redirected.
<BR></P>
<P>Earlier in this chapter, you saw the following example with a space between the partial filename and the metacharacter:
<BR></P>
<PRE>$ ls LINES. *
LINES.: not found
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p10 p101 p11
t11 z11</PRE>
<P>It appears that all of the output in this example is on the standard output. However, if you change the command slightly, you get different results:
<BR></P>
<PRE>$ ls LINES. * >dirfile
LINES.: not found</PRE>
<P>What has happened is that the legitimate output from the ls command has been redirected to dirfile, and the error message has been sent to the standard error file.
<BR></P>
<P>To redirect error messages, use the > operator prefixed with a 2. For example,
<BR></P>
<PRE>$ ls LINES. * 2>errmsg
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p10 p101 p11
t11 z11</PRE>
<P>Now the error message has been directed to the file errmsg, and the legitimate output has gone to the standard output file.
<BR></P>
<P>You can redirect both standard output and standard error for the same command. For example,
<BR></P>
<PRE>$ ls LINES. * >dirfile 2>errmsg</PRE>
<P>You cannot redirect the same standard file twice. For example, the following command is invalid:
<BR></P>
<PRE>$<B> </B>ls >dirfile >anotherdir</PRE>
<P>If you wanted to discard all error messages, you could use the following form:
<BR></P>
<PRE>$<B> </B>ls LINES. * >dirfile 2>/dev/null</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> The standard error redirection operator (2>) is actually the same operator as standard output redirection (>). When a UNIX program opens files, they are given integer numbers. The three standard
files are numbered 0, 1, and 2.
<BR>
<BR>0 is assumed for input redirection. 1 is assumed for output redirection; therefore, redirection of standard output can also be written as 1>. Redirection is not restricted to only the first three files. However, to redirect higher-numbered files,
the user would need to know how they are used within the program.
<BR></NOTE>
<HR ALIGN=CENTER>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Note for C shell users. In the C shell, error messages cannot be redirected separately from standard output. In the C-shell you can include error output with standard output by adding an ampersand
(&) to the redirection symbol.
<BR>
<BR>$ ls LINES. * >& dirfile
<BR>
<BR>This command would redirect both standard output and error messages to dirfile.
<BR></NOTE>
<HR ALIGN=CENTER>
<H3 ALIGN="CENTER">
<CENTER><A ID="I34" NAME="I34">
<FONT SIZE=4><B>Using Pipes to Pass Files Between Programs</B>
<BR></FONT></A></CENTER></H3>
<P>Suppose that you wanted a directory listing that was sorted by the mode—file type plus permissions. To accomplish this, you might redirect the output from ls to a data file and then sort that data file. For example,
<BR></P>
<PRE>$ ls -l >tempfile
$ sort <tempfile
-rw-rw-r— 1 marsha adept 1024 Jan 20 14:14 LINES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 LINES.idx
-rw-rw-r— 1 marsha adept 256 Jan 20 14:14 PAGES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 PAGES.idx
-rw-rw-r— 1 marsha acct 240 May 5 1992 acct.pds
-rw-rw-r— 1 marsha adept 1024 Nov 22 15:42 marsha.pds
-rw-rw-r— 1 marsha adept 0 Jan 21 10:22 tempfile
-rwxr-xr— 1 asm adept 512 Dec 14 16:16 21x
-rwxrwxr— 4 root sys 243072 Aug 22 1991 p11
-rwxrwxr— 4 root sys 256041 Aug 22 1991 t11
drw-rw-r— 1 marsha adept 3072 Oct 12 11:42 users</PRE>
<P>Although you get the result that you wanted, there are three drawbacks to this method:
<BR></P>
<UL>
<LI>You might end up with a lot of temporary files in your directory. You would have to go back and remove them.
<BR>
<BR></LI>
<LI>The sort program doesn't begin its work until the first command is complete. This isn't too significant with the small amount of data used in this example, but it can make a considerable difference with larger files.
<BR>
<BR></LI>
<LI>The final output contains the name of your tempfile, which might not be what you had in mind.
<BR>
<BR></LI></UL>
<P>Fortunately, there is a better way.
<BR></P>
<P>The pipe symbol (|) causes the standard output of the program on the left side of the pipe to be passed directly to the standard input of the program on the right side of the pipe symbol. Therefore, to get the same results as before, you can use the
pipe symbol. For example,
<BR></P>
<PRE>$ ls -l <B>|</B> sort
-rw-rw-r— 1 marsha adept 1024 Jan 20 14:14 LINES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 LINES.idx
-rw-rw-r— 1 marsha adept 256 Jan 20 14:14 PAGES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 PAGES.idx
-rw-rw-r— 1 marsha acct 240 May 5 1992 acct.pds
-rw-rw-r— 1 marsha adept 1024 Nov 22 15:42 marsha.pds
-rwxr-xr— 1 asm adept 512 Dec 14 16:16 21x
-rwxrwxr— 4 root sys 243072 Aug 22 1991 p11
-rwxrwxr— 4 root sys 256041 Aug 22 1991 t11
drw-rw-r— 1 marsha adept 3072 Oct 12 11:42 users</PRE>
<P>You have accomplished your purpose elegantly, without cluttering your disk. It is not readily apparent, but you have also worked more efficiently. Consider the following example:
<BR></P>
<PRE>$ ls -l <B>|</B> sort >dirsort & ; ps
PID TTY STAT TIME COMMAND
13678 003 R 2:13 sh
15476 003 R 0:01 ls
15477 003 R 0:00 sort
15479 003 R 0:00 ps</PRE>
<P>Both ls and sort are executing simultaneously, which means that sort can begin processing its input, even before ls has finished its output. A program, such as sort, that takes standard input and creates standard output is sometimes called a filter.
<BR></P>
<P>The capability to string commands together in a pipeline, combined with the capability to redirect input and output, is part of what gives UNIX its great power. Instead of having large, comprehensive programs perform a task, several simpler programs can
be strung together, giving the end user more control over the results. It is not uncommon in the UNIX environment to see something like this:
<BR></P>
<PRE>$<B> </B>cmd1 <infile | cmd2 -options | cmd3 | cmd4 -options >outfile</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I35" NAME="I35">
<FONT SIZE=4><B>Summary</B>
<BR></FONT></A></CENTER></H3>
<P>In this chapter, you learned how to use UNIX commands to list filenames with ls and to locate files based on search criteria with find. You also learned how to supply partial filenames to a command by using filename substitution. Finally, you learned
how to reroute input and output by using standard file redirection and piping.
<BR></P>
<P><A HREF="unx03.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx05.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -