| UNIX Tutorial | 10. Redirecting input/output |
|
Commands covered in this section: mail For every process in UNIX, three files are automatically opened: standard input, standard output, and standard error. (For the purposes of this section of the tutorial, you can think of a process as a program you run from the shell, such as ls or who.) Standard input (sometimes abbreviated as stdin) is usually mapped to the keyboard. This means that the process will look for its input from the keyboard by default. Standard output (sometimes abbreviated as stdout) is usually mapped to the screen. By default, the process will send its normal output to the screen. Standard error (sometimes abbreviated as stderr) is similar to standard output, except that it is reserved for error messages. You can use redirection to send the input or output of a process to a location other than the standard one. The "greater than" symbol ('>') is used to redirect the standard output of a process. By placing '>' after a command and following this with the name of a file, the output of the command will go to the specified file instead of the screen. Exercise 10.1
Repeat the who command covered in section 7 and
redirect the output to a file.
It is not necessary to create the output file ("who.out" in
the example above) before doing the redirection. It will be
created automatically when the command is run. Note that if
the file does exist before the redirection takes place,
using '>' will cause any existing contents in the file to
be overwritten. In other words, if you have a file called
"myfile" with something important in it (let's say it's
your doctoral dissertation), you may want to think twice
before running a command like "who > myfile". Some systems
are configured to prevent this (it's called "noclobber")
by default. In that case, the redirection will fail and
you will receive an error message saying the file exists.
If you would like the output of a program to be appended to a file, as opposed to overwriting it as discussed above, you can use '>>' to redirect the output to a file. Exercise 10.2
Repeat the who command once again, this time
using '>>' to redirect the output.
As you might expect, the "less than" ('<') symbol is used to
redirect the standard input of a process. By placing '<' after
a command and following this with the name of a file, the input
for the command will be taken from the specified file instead
of the keyboard.
Exercise 10.3 Take the output from the who command which was redirected to a file in the previous exercise and mail it to yourself with the mail command.If you have trouble figuring out which redirection symbol to use, just think of the '<' and '>' symbols as arrows. If the arrow is pointing from a filename to the program (as in "mail joe@domain.com < myfile"), you are redirecting the input. If the arrow points from the program to a filename (as in "who > myfile"), you are redirecting the output.% mail -s "redirection" username@domain.com < who.outAgain, note that the order is important. Here, the command is 'mail -s "redirection" username@domain.com'. This is followed by the input redirection symbol ('<'), then the name of file containing the input to be sent to the program. |
|
|
< previous | table of contents | next > |
|