Standard IO


Standard I/O

Now that you understand what I/O is, and how commands in Unix work with redirection, it's time to cover what standard input and output is, as well as standard error. In this section of the lesson, we will cover how Unix accepts input and output and errors by default.

When each Unix program is created, it will have a way to accept input. Unix itself is not only based on I/O, but so is every program that runs within it. Unix, when operated through the shell prompt, allows you to control I/O completely, which is why such characters can be used in shell scripts, which will be covered in Lesson 14, "Shell Scripting Fundamentals." In the last lesson we learned how to use cron to automate, and it could automate a script. Think of the shell prompt commands and processes we have learned. It should be clear to you that if you master what we have learned, and shell scripting, the power of Unix dwarfs its competitors. Unix is flexible in just about every way imaginable. One of the things you can do with I/O management is redirect input from any standard location to anything you specify.

As mentioned, you can redirect input and output to come from or go to a file, but that is not the only way I/O can be used. You can also send the contents of a file to someone as an email. It is virtually limitless as to what you can do when learning how to manage I/O; the secret is in learning the commands and characters and mastering how they can be used.

You can also hook up programs into a pipeline through a pipe, in which the standard output STDOUT of one program feeds directly into the standard input STDIN of another. An example of this could be seen if you were to send your email output to a printer directly. Pipes will be covered in the last section of this lesson.

STDIN

STDIN stands for standard input. The input connection for a program is called STDIN. A program can expect the incoming data stream from the user (or wherever) to appear at STDIN. User input is read from STDIN. As just mentioned, data can be redirected or piped into a program's STDIN from any source; this was shown with the examples of the cat command. The normal input for a program is generally taken from the most common of input peripherals, the keyboard. Remember our last example?

 > cat  < testfile1  >  testfile2 

STDIN, when used with cat as it is in this example, changes the default input channel to specify a different place to get input from, which happens to be testfile1 instead of the default, the keyboard.

When you interact with a shell prompt command (like cat), the program is reading the data you are entering from STDIN. If you prefer not to enter the data by hand, you can put it in a file and redirect the file into the program's STDIN. The < character directs the contents of the file to its right into the STDIN of the command to the left. Although this is confusing, if you keep reading this over and over and try to apply the concepts to the examples given, I promise you that it will eventually make more sense. Just make sure you are learning the concepts and are able to apply them on this small level. If you can, then move on to more complex command structures, and scripts will almost become commonplace to you. Let's take a look at standard output, STDOUT.

STDOUT

STDOUT stands for standard output. STDOUT is the output connection that Unix provides for programs. Just as you can redirect STDIN from a file, if you want to send the output of a command to a file, then you can redirect STDOUT. The > character directs the STDOUT of the program to its left into the contents of the file to its right. In our same example, we look at using the cat command to copy the contents of one file to the contents of another. Commonly, as you saw with cat, used with no argument, its default output was to echo the command back to the terminal. The standard input was the keyboard. Now, the STDOUT has been altered. We learned in the last example that the input (STDIN) was changed to testfile1. Now the standard output (STDOUT) has been changed to testfile2. Getting easier, right?

 > cat  < testfile1  >  testfile2 

Again, the standard output is the default output stream that messages are sent to (commonly the end user's terminal). The previous statement, however, redirects the output to testfile2.

Overwrite or Append? Using > to redirect output to a text file will overwrite the file if it currently exists (that is, it will replace the file with the new output and the old stuff just goes away). This sort of behavior is not always what one wants, so Unix provides the >> operator as well. Using >> will append, or add on to the end of the existing file any new output redirected to it.


Next up we will learn about the third standard form of I/O, the standard input error, or standard error (STDERR) for short.

STDERR

STDERR is another output stream beyond STDOUT, and it's like that for a good reason. You want the two data streams separated. We already covered STDOUT; it is the first. The second, STDERR, stands for standard input error. STDERR is the output stream that error messages are sent to in Unix. This is commonly the end user's terminal.

If the user is redirecting STDOUT and the program can only put errors on STDOUT, the user might never see the errors that all go into the redirected file. Instead, programs can use STDERR for errors. If the user has not redirected it, then he can still see error messages and warnings while STDOUT is headed into another file or program.

If you want to put STDERR into the same file in which you're storing STDOUT, use >& instead of > in the command; you can do so as follows:

 > cat < newfile1 >& newfile2 

Now that we are comfortable with the basics of standard I/O, let's take a look at how to use pipes in Unix.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net