Input and Output Redirection
Input and Output Redirection
Input and Output Redirection
Module 9
Introduction One important thing you have to know to understand I/O redirection is file descriptors. There are three types of I/O, called a file descriptor. They are standard input, standard output, standard error. Topics Input Redirection Output Redirection Error Redirection Filter Pipes
Input Redirection
The standard input (stdin) by default is any text entered from the
keyboard.
Standard input can be redirected through the use of command line
argument and redirection operators (like <) so that it becomes a source other than the keyboard argument.
The input redirection is generally useful if you have a data file and a
Contd
Input Redirection
Example of input redirection
Output Redirection
The default standard output (stdout) is the display screen.
screen.
Standard output can be redirected through the use of command line
the printer.
Contd
Output Redirection
Example of output redirection # ls > list.file
Error Redirection
By default the standard error (stderr) displays on the terminal and can
Standard error can be redirected with the standard error redirection operator (i.e 2>, 2>>).
The error messages can be redirected to any file, so that the error
messages will not appear on the screen and it is written to the file.
Contd
Error Redirection
Example of error redirection # ll myfile 2>error_message
> >>
> operator sends the input of command or file to the other file as standard input, overwriting the existing file. > > operator sends the input of command or file to the other file as standard input, appending text to an existing file, instead of overwriting the file.
Contd
Contd
Contd
Filters
Filters take standard input and perform an operation upon it and
Example for filters: grep root /etc/passwd The grep command is used to filter the root user from the /etc/passwd file.
Pipes
A pipe is a mechanism for interprocess communication.
The data written to the pipe by one process can be read by another
to another command.
Contd
Pipes
The tee command
The tee command copies the standard input to standard output and also to any files given with an argument.
This is useful when you want not only to send some data down a pipe, but also to save a copy. If a file being written which does not already exist, it is created.
If a file being written which already exists, the data contained is overwritten unless the -a option (appends the standard input to the given files rather than overwriting)
Contd
Pipes
Example of tee command
Lab Exercise
Filter and display the word blue from the file my_sortfile