Sunday, January 8, 2017

Standard Streams 1

Standard Streams 1

What is standard input, output and error ?

Each UNIX program has a standard input, a standard output, and a standard error channel.

Programs can import entries from the standard input, as well as from the user or from a file.

If the output of the programs is written in such a way as to use the standard output, the user can easily transfer the output in a different environment that the user wishes, such as screen, printer, file.

Example: The "ls -l> file_list.txt" command writes the output of the running "ls" application to file file_list.txt.

Software that receives data from standard input, processes them, and directs output to standard output are also called "filters". Filter software can perform complicated operations when properly appended to each other.


Each program communicates over 3 channels.Standard INPUT channelStandard OUTPUT channelStandard ERROR channelEach communication channel has a system-assigned number. These numbers are 0 for standard input, 1 for standard output, and 2 for standard error.The program retrieves the information that the user wants from the standard input channel 0, ie under normal conditions, from the keyboard.The program generates input jobs and processed information from the user. This information is also displayed to the user from the standard output channel 1. Under normal circumstances, the program displays the processed information on the screen.The progam can make some mistakes while receiving input from the user. The standard error channel 2 is used to notify the user of unusual situations like this. Program errors are displayed to the user via the standard error channel unless otherwise specified. Errors that occur under normal conditions are displayed on the screen.Although the standard error and standard output device have the same screen, they can be separated easily because they are transmitted from different channels and they should be considered separately.

Normally, when a program is run, it writes its processed information to the screen on channel 1, which is the standard output channel.However, at the end of the program, when the characters "1>" and immediately following the file name are specified, the information written to the standard output channel is written to the specified file. Hence, the standard output channel is disconnected from the screen and the output of the program is directed to the file.Assuming the character ">" is used instead of "1>", the system assumes channel 1 is the standard output channel. In other words, if the channel number is not specified in the redirection operator, this is the standard output channel number 1.If the standard output made with the characters "1>" and ">" is directed to the file, if there is a file with the same name in the specified location, the contents are lost without any warning and the new content is written to the output file.If the output to be created is to be added behind an existing file, the "1 >>" or ">>" characters must be used to direct the file. The system understands that the standard output is appended to the end of the file, and adds the output generated by the program immediately after it does not damage the contents of the existing file.If the file does not exist, the file is automatically created by the system, directing it to the file by appending or overwriting it. 

Example Usage: 

program > file.txt
program 1> file.txt
program >> file.txt
program 1> file.txt  

Normally, when a program is run, the errors and errors that occur during data processing are written to the screen from channel 2, which is the standard error channel.However, at the end of the program, when the characters "2>" and immediately following the file name are specified, errors written to the standard error channel are written to the specified file. Therefore, the standard error channel is disconnected from the display and program errors are directed to the file.If the standard error made with the characters "2>" is redirected to the file, if there is a file with the same name in the specified location, the contents are lost without any warning and program errors are written to the file. If the error to be created is to be added behind an existing file, the "2 >>" characters must be used to redirect the file. The system understands that the standard error is to be appended to the end of the file and adds the error generated by the program immediately after it does not damage the contents of the existing file.If the file does not exist, the file is automatically created by the system, directing it to the file by appending or overwriting it.

Example Usage: 

program 2> file.txt
program 2>> file.txt

The "2> & 1" characters must be written at the end of a command in order for a program to combine standard output and standard error channels and write it on the same channel over a single channel.

It is pointless to merge standard output and standard error channels into a single channel as long as the resulting combined output is not redirected to a file.

The standard output and standard error channels combination operand "2> & 1" must be used after a standard output is directed to a file so that the standard output and standard error can be combined and directed to a file.


Example usage:

program 1> file.txt 2>&1
program 1>> file.txt 2>&1 

To read my previous article: Disk Management 2

No comments :

Post a Comment

Comments system

Disqus Shortname