Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


STDERR

The standard error stream, or STDERR, is where error messages are sent. As you enter more complex commands on a shell, you’ll 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, let’s 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 Redirection

If 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 Program’s Output Is Another’s Input

The 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 don’t 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 you’d 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 isn’t much we can do yet.


Check This Out:  Throwing unwanted output away
Sometimes you don’t want to see the output from a program on your display, but you also don’t want to send it to a file. Maybe you just don’t want it at all. For example, when listing all of the files in your system:
ls -R / > filelist.txt >
 error.txt

You know that this command sends a list of all the files in your computer to filelist.txt, and the access errors it encounters to error.txt. What if you only want to save or know about the errors? Is there any way to throw the long list of files away as the command runs?

UNIX computers all have a special file in the /dev directory called /dev/null, also commonly referred to as the bit-bucket. Any data sent to /dev/null in a stream is thrown away instantly. To throw away the list of files and just keep the errors, you’d type:

ls –R / > /dev/null 2>
 error.txt

The errors will still be saved in error.txt, but now you don’t have to deal with the long list of files.


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


Previous Table of Contents Next


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.