Standard stream and redirection in Unix

Standard stream and redirection in Unix

Explain redirection in Unix? How are redirection of pipes used? Explain with an example?

Redirection: Unix defines three standard stream that are used by commands each command takes it input from a stream known as standard input command that creates output send it to the stream is known as standard output. if an executing command  encounter an error the error message is sent to standard error so unique assign descriptor to each stream to reference them

  1. The descriptor for standard input is 0
  2. The descriptor for standard output is 1
  3. The descriptor for standard error is 2.


The default physical file associated with each stream:

  1. Standard input is associated with the keyboard
  2. Standard output is associated with the monitor
  3. Standard error is also associated with the monitor

Each command may use standard input stream ,standard output stream and standard error stream. these strange assigned to the keyboard and the monitor whenever necessary we can change the default assignment temporary using redirection

Types of redirection in Unix

There are three types of redirection in Unix

  • Input redirection
  • Output redirection
  • Error redirection

Input redirection

we can redirect the standard input from the keyboard to and text file the input redirection operator is less than character (>). Arrow pointing to a command, meaning that the command is to get it input from the designation file. There are two ways to express the redirectionthe first method explicitly specify that the redirection is applied to a standard in by coding the descriptor. The second method omaxe the descriptor because there is only one standard input. We can omit it.also there should be no space between the descriptor and the redirection symbol.


$command 0<file1

Or

$command<file1

Output redirection

when we redirect standard output the command output copied to the file rather than displayed on the monitor.


There are two basic redirection operator for standard output both start with a greater than character (>). they putting away from the command and towards the file that is to receive the output, which of the operator is used depends on how the output file is handles. if you want the file to contain only the output from the execution of the command we use one greater than token (>) in this case when we redirect the output to a file that doesn't exist. Unix create read and write the output if the file already exist the action depends on the setting of the Unix option known as noclobber. When the option is turned on its present redirected output from destroying and existing false in this case we get an error message.

if you want to overwrite the option and replace the current file content with the new output we must use the redirection override operator greater than bar (> | )

Error redirection: one of the difficulties with the standard error stream is that it is by default combined with the standard output stream on the monitor in the following example we use the long list (ls) command to display the permissions of two files if both and valid one displays after the other. If only one is valid, it is displayed, what is this place and error message for the other one on the same monitor.



Post a Comment

Previous Post Next Post