Standard Streams 2
PIPE
In UNIX, pipe operation is to direct a standard output of a command to be the standard input of another command. The standard output of a program can be redirected to another program
as a standard input, the standard error can only be redirected to a
file.The
"bc" program receives input from the keyboard. In processing this
input, the generated errors are written to the screen via the standard
error channel, and the results generated during the input process are
also given to a pipeline instead of the standard output device monitor. The standard output of the "bc" output from the pipeline is given as standard input to the command "wc -l". "wc
-l" processes the standard input from the pipeline and sends the errors
to the screen via the standard error channel, while the normal results
are sent to the same screen via the standard output channel. In this way, UNIX commands can be connected to the end-point to perform very meaningful and challenging tasks easily.
Example usage of pipe:
Example commands using pipe (|):
program1 | program2 : Standard output of the program1 is redirected to program2 using pipe.
program1 2> errors.txt | program2 : Standard output of the program1 is redirected to program2 using pipe. Also standard error of program1 is redirected to a file named errors.txt
The operators "<", ">", ">>", "2>", "2>&1" are used to direct input and output.
These operators are placed between a command set (command and parameters) and a file, and input and output are directed.
In output directories made using ">", the output file overwrites the file if it already exists. The ">>" operator can be used to prevent this and add it to the file.
If the file to which the output is directed does not exist, it is created automatically.
command > file.txt 2>&1 : both standard error and standard output are merged into the "file.txt" file.
"tee" Command
The "tee"
command copies the standard input as if it were a standard output, and
at the same time writes the standard input to the specified file.
In general, it is used to transfer the intermediate results of the commands connected to the end via the boring method.
The "tee" command overwrites the specified file under normal circumstances. However,
if the contents of the file need to be lost, and the backend is
desired, the "tee" command should be used with the "-a" option in the
form "tee -a file".
To read my previous article: 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