See this page as a slide show
More Redirection
CS155 MoreRedirection
Streams
- When a Unix program is started a set of associated streams are opened.
- stdin – standard input
- stdout – standard output
- stderr – standard error
- These streams allow us to manipulate the input and output of a Unix program.
Streams and Redirection
- We have already seen how to send data to stdin using
<
- Also, we can attach stdout to a file or program using
>
, >>
, or |
- Error messages go to stderr, not stdout.
Redirecting stderr and stdout
% ls
% rmdir alpha > output
rmdir: failed to remove 'alpha': No such file or directory
- Why didn’t the error message go to the file
output
?
- Redirect just stdout with
>
or just stderr with 2>
or both stdout and stderr with >&
.
Example
% date >now
% ls -l
total 4
-rw------- 1 cs155 class 29 Dec 22 02:34 now
% ls -l now then
/bin/ls: cannot access 'then': No such file or directory
-rw------- 1 cs155 class 29 Dec 22 02:34 now
% ls -l now then >output
/bin/ls: cannot access 'then': No such file or directory
% ls -l now then >&output
Summary
- command
-
Standard output and standard error both go to the terminal.
- command
>
file -
Standard output goes to file. Standard error goes to the terminal.
- command
>&
file -
Standard output and standard error both go to file.
- command1
|
command2 -
Standard output of command1 becomes the standard input of command2.
- command1
|&
command2 -
Standard output & error of command1 become
the standard input of command2.