IO Redirection


I/O Redirection

With Unix, I/O redirection is defined as nothing more than sending input or output to somewhere other than the default locations, specifying a different or alternative destination. Input and output redirection is done with special characters.

It should be noted that every Unix shell offers I/O redirection with a standard set of characters used to achieve it. Those characters are

<, which redirects standard input to come from a file

>, which redirects standard output to go to a file

There are other characters that we will learn about, but for now, these are the two most common forms of I/O redirection known or used.

We will look at an example of using these characters with the cat command. We learned about this command when we learned how to read files in Unix.

The cat command is actually short for concatenate. Concatenate means "to link together" and is the perfect definition for the examples I am about to show you. The cat command will allow you to specify multiple filename arguments, and then cat will copy them to standard output, better known as STDOUT. We will cover this shortly. When using cat, remember that we used the 'command < filename' syntax here. This means that you want to change what was standard output for cat and change it to what you specify. Simple, right? If standard behavior for running cat without any arguments is to just repeat any input back to the terminal, then cat > filename can change how I/O is directed so that in this case all the input will be directed into the file.

 >cat 1 1 2 2 Crtl+d 

In this example, we showed that the cat command, when used without a filename argument, simply copies its current input to its output. This is why when you typed 1, cat was nice enough to repeat it back to you. This proves a point about input and output. You invoked the cat command, it waited for your input, and then repeated it as output. This should solidify what I/O really is within Unix at its most fundamental level.

To show how redirection works, let's take the cat command one step further. We covered the usage of both the characters; now let's see them in action. We already learned how to copy a file from one location to another with the cp command; now we will learn how to do it through redirection with cat.

 > cat  < testfile1  >  testfile2 

The concepts of I/O, redirection, and the usage of special characters should start to make more sense from these simple examples. One last note to be mentioned is that input and output redirectors can be combined. We saw this in the last example where we used the first character to change its input, and then the second character defined the output. Both the standard input STDIN and output STDOUT were changed from their defaults. Both of these terms will be covered next.



    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