Standard Input and Standard Output


It may sound complicated, but it isn't. Standard in (STDIN) is where the system expects to find its input. This is usually the keyboard, although it can be a program or shell script. When you change that default, we call it redirecting from STDIN.

Standard out (STDOUT) is where the system expects to direct its output, usually the terminal screen. Again, redirection of STDOUT is at the discretion of whatever command or script is executing at the time. The chain of events from STDIN to STDOUT looks something like this:

 standard in  -> Linux command  ->  standard out 

STDIN is often referred to as fd0, or file descriptor 0, while STDOUT is usually thought of as fd1. There is also standard error (STDERR), where the system reports any errors in program execution. By default, this is also the terminal. To redirect STDOUT, use the greater-than sign (>). As you might have guessed, to redirect from STDIN, you use the less-than sign (<). But what exactly does that mean? Let's try an experiment. Randomly search your brain and pick a handful of names. Got them? Good. Now type the cat command and redirect its STDOUT to a file called random_names:

 cat > random_names 

Your cursor will just sit there and wait for you to do something, so type those names, pressing Enter after each one. What's happening here is that cat is taking its input from STDIN and writing it to your new file.

You can also write the command like this:

 cat - 1> random_names 

The hyphen literally means standard in to the command. The 1 stands for file descriptor 1. This is good information, and you will use it later. Finished with your random names list? When you are done, press <Ctrl-D> to finish. <Ctrl-D>, by the way, stands for EOF, or end of file.

 Marie Curie Albert Einstein Mark Twain Wolfgang Amadeus Mozart Stephen Hawking Hedy Lamarr ^D 

If you cat this file, the names will be written to STDOUT in this case, your terminal window. You can also give cat several files at the same time. For instance, you could do something like this:

 cat file1 file2 file3 

Each file would be listed one right after the other. That output could then be redirected into another file. You could also have it print out the same file over and over (cat random_names random_names random_names). cat isn't fussy about these things and will deal with binary files (programs) just as quickly. Beware of using cat to print out the contents of a program to your terminal screen. At worst, your terminal session will lock up or reward you with a lot of beeping and weird characters.

Quick Tip

If you get caught in such a situation and all the characters on your screen appear as junk, try typing echo and then pressing <Ctrl+V> and <Ctrl+O>. If you can still type, you can also try typing stty sane and then pressing <Ctrl+J>. Some systems also provide a command called reset that will return your terminal session to some kind of sane look.


Redirecting STDIN works pretty much the same way, except you use the less-than sign instead. Using the sort command, let's take that file of random names and work with it. Many commands that work with files can take their input directly from that file. Unless told otherwise, cat and sort will think that the word following the command is a filename. That's why you did the STDIN redirection thing. Yes, that's right: STDIN is just another file. Sort of.

 sort random_names 

The result, of course, is that you get all your names printed out in alphabetical order. You could have also specified that sort take its input from a redirected STDIN. It looks a bit strange, but this is perfectly valid:

 [mgagne@scigate tmp]$ sort < random_names Albert Einstein Hedy Lamarr Marie Curie Mark Twain Stephen Hawking Wolfgang Amadeus Mozart 

One more variation involves defining your STDIN (as you did previously) and specifying a different STDOUT all on the same line. In the following example, I am redirecting from my file and redirecting that output to a new file:

 sort < random_names > sorted_names 



Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2003
Pages: 247

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