![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Complete Idiot's Guide to Linux
STDERRThe standard error stream, or STDERR, is where error messages are sent. As you enter more complex commands on a shell, youll probably want to have diagnostic or warning messages sent to a log file. Redirecting error messages to a new file is especially useful if you are debugging a shell script. Redirecting Errors By default, both STDERR and STDOUT point to your console so you can see diagnostic messages. When you redirect STDOUT, STDERR still points to your console, and any errors are still printed there. To redirect errors, you need to specify a file number. The input, output, and error streams have assigned file numbers in the shell: STDOUT is 0, STDIN is 1, and STDERR is 2. You can combine the file number and the redirection operator. To redirect errors to a different place, you specify the redirection like this: command > outputfile 2> errorfile Messages printed to the standard error stream, STDERR, will go to errorfile, and standard output to STDOUT will redirect outputfile. For example, lets say you wanted to create a list of all the files on your system. Depending on your user id, you might not have permission to go into some directories. The following command line will redirect permission access errors to a different file while capturing good output in a separate file: ls -R / > filelist.txt 2> error.txt Command Groups and RedirectionIf you wanted to redirect the output of multiple commands, it would seem reasonable to think that you could use a command like this: pwd; cd ..; pwd; ls -l > /tmp/listing However, that command line only redirects the output of the last command. To redirect output for all of the commands, you need to create a command group and redirect standard output for the entire group instead: (pwd; cd ..; pwd; ls -l) > /tmp/listing This places the output of each program in succession into the /tmp/listing file. Pipes: One Programs Output Is Anothers InputThe true power of shells shines when the output from one program becomes the input of another whose output then becomes the input of yet another, and so on. You can interconnect the input and output of a number of programs in a cascade of useful information. At this point, you might be envisioning a scheme by which you use a series of input and output redirection commands, passing information through a temporary file. But in a more direct fashion, you can interconnect programs with pipes. Pipes are very useful: They allow you to use the output stream of one program as the input stream of another without the need for temporary files. For example, if you want a list of all users currently logged into the system, you can use the who command. The who command displays information about each active user, one user per line. If you merely want to know how many users are on the system, but dont need all of the extra information who provides, you need to resort to some trickery. UNIX has a command called wc that counts the number of words, lines and characters that a file contains. Knowing that who outputs information about one user per line, you quickly realize that youd like wc to count the number of lines who displays. You can accomplish this by connecting who and wc together with a pipe: [alberto@digital alberto]$ who | wc -l 1 As you can see, the who and wc commands can be piped by connecting them with a vertical bar (|). You can tie together as many programs as you want. However, given our limited command vocabulary, there isnt much we can do yet.
One common use of pipes is in conjunction with the ls command. If you list a directory that contains many files, such as the /dev directory, the output will scroll past your screen more quickly than you can see it. What do you think the solution is? If your thought is to make a pipe between ls and a page such as less, you got it! The command you would use to list the /dex directory is this: ls -l /dev | less
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |