Unix programs (invoked via a command line) generally make use of "standard input" (the keyboard by default) and "standard output" (the console window by default). It is possible to "redirect" the output into a file or "pipe" the output of one program into the input of another. It is also possible to "redirect" the program to use a file as "standard input" instead of the keyboard. Some examples should make it clear how this is done. Typing
cat in.txt
will print the contents of the file in.txt to the standard output. In this case that is the console (screen). You can instead send the ouput into another file like this
cat in.txt > out.txt
Now in.txt and out.txt will contain the same data. This is not the recommended way to copy a file but it works. Here is another example. Typing
ls
prints the directory listing. Typing
ls > myfiles
creates a file "myfiles" and puts the listing in the file. The ">" symbol redirects standard output into the file that follows the ">". Redirecting standard input is the same but uses "<". The program "spell" reads standard input and echoes any words that are misspelled. Here is a brief interactive session with spell.
unix258 :spell this is a tst type control-d when done entring data ^D entring tst unix259 :
We could also spell check an entire file with:
spell < somefile
You can of course combine both in a single command so
spell < file2check > errors
will put the misspelled words from file2check into errors. If you wanted to know how many words were misspelled you could then run "wc" on the errors file.
wc errors
But "wc" can also read from standard input so the above could also be written
wc < errors
This then sets us up for the use of a Unix "pipe" indicated with the symbol "|". The "pipe" connects the standard output of one program to the standard input of another. Using "|" we could count the number of misspelled words in file2check with a single command.
spell < file2check | wc
The redirects file2check to be the standard input for spell, and then pipes the standard output of spell into the standard input of wc.