The document discusses the three standard files - standard input, standard output, and standard error - that are associated with terminals in Unix/Linux systems. Standard input represents keyboard input, standard output represents terminal display output, and standard error represents error messages. Redirection operators like < and > allow redirecting these standard streams to files or other programs.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
54 views
Redirection: The Three Standard Files
The document discusses the three standard files - standard input, standard output, and standard error - that are associated with terminals in Unix/Linux systems. Standard input represents keyboard input, standard output represents terminal display output, and standard error represents error messages. Redirection operators like < and > allow redirecting these standard streams to files or other programs.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10
Redirection: The Three Standard Files
In the context of redirection, the terminal is a generic name
that represents the screen, display or keyboard (or even an X window that emulates a terminal) We see command output and error messages on the terminal (display), and we sometimes provide command input through the terminal (keyboard). The Shell associates three files with the terminal- two for the display and one for the keyboard. These special files are actually streams of characters which many commands see as Input and Output. When a user logs in, the shell makes available three files representing three streams. Each stream is associated with a default device, and – generically speaking-this device is the terminal: Standard Input: The file ( or stream) representing input, which is connected to the keyboard. Standard Output: The file ( or stream) representing output, which is connected to the display. Standard Error: The file (or stream) representing error messages that emanate from the command or shell. This is also connected to the display. Standard Input e.g. cat and wc commands have an additional method of taking input. When they are used without arguments, they read the file representing the standard input. This file is indeed special; it can represent three input sources: The keyboard, the default source. A file using redirection with the < symbol. Another program using a pipeline. Standard Output All commands displaying output on the terminal actually write to the standard output file as a stream of characters, and not directly to the terminal as such. There are three possible destinations of this stream: The terminal, the default destination. A file, using the redirection symbols > and >> As input to another program using a pipeline. Standard Error Redirecting standard error requires the use of the 2> symbols. $cat file1 2 > errorfile The > operator tells Unix/Linux, “Don’t display this output on screen instead, put it somewhere else”. Likewise the < operator says, “The input for this command is not coming from the keyboard this time look for it somewhere else.” The “somewhere else” can be a file or a printer for output redirection and a file or a keyboard for input redirection. Consider the command: $ cat file1 > file2 On executing this we will promptly be returned to the $ prompt. The redirection operator declares file2 to be the standard output, ousting the terminal screen from that role. Thus the output of cat which is normally sent to the screen is now sent to file2 and not to the screen. If the file file2 does not exist, it is created. If it does exist, it is wiped clear and refilled with the new data. Thus, the output going to the screen has been redirected to a file. Any command that accepts standard input also accepts input redirection. $ cat < newfile Here the file newfile became the standard input, and cat read its contents and displayed them on the screen. The same thing could as ell have been achieved by saying: $ cat newfile We may use both the redirection operators at once: $ cat < currentfile > newfile The first part of redirection, < currentfile, indicates that input is to be taken from the file currentfile, and the second part of redirection, > newfile establishes that output is to be routed to the file newfile. The order in which the two indirection operators are used doesn’t matter. We could have obtained exactly the same effect even by saying: $cat > newfile < currentfile What would be the Output of the following:- $ cat par3 par4 par5 >> report $ cat file > file1 $ cat < file > file1 $ cat > file < file1 $ date; who $ date; who > logfile $ (date;who) > logfile