Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
Text flow
Files are used for data storage, which is equivalent to a house that stores data. We said earlier that the so-called data is a sequence of 0 or 1, but strictly speaking, Linux is used as a unit of data in bytes (byte), which means that the sequence is one unit per eight bits (bit) (eight-bit binary corresponds to a decimal range of 0 to 255). Using ASCII encoding, you can convert such a byte into a character. So, in Linux, the data that we say can be expressed in characters, that is, the form of text.
In fact, if the characters are processed in bits, the machine will be easier to read and transmit, and will be more efficient. But why is Linux still being processed in bytes? The reason for this is that data can be converted to characters more easily in byte units relative to the data being processed in bits. Characters are easier to read than dull 0 and 1 (Human readable). However, not all of the data is designed to be readable, such as the various characters contained in the executable file are meaningless to the person (because the executable is for the machine to read). However, Linux still processes all files in bytes, so that all files can share a single set of interfaces (virtual file system), reducing the complexity of the Linux design.
("Everything is a file" was one of the most common philosophies of Unix design, but Linus corrected it and changed it to "everything is a stream of bytes". )
However, the data is not settled forever after finding its own house (file). It is often read into memory (like going to work in the office), or transferred to an external device (like a hotel vacation), or moved to another house. In such a move, the data is like a queue of people walking, we call it text stream (text stream, or byte stream). However, the connection method between different devices of the computer varies greatly, from memory to file connections like climbing mountains, from memory to peripherals like swimming across a river. To do this, Linux also defines a stream, which is the standard for building highways that connect everywhere. The advantage of stream is that all highways are the same whether you are from memory to peripherals or from memory to files (there is no need to worry about the stone or the land below the road).
Let's take a second look at the phrase "everything is a stream of bytes". Information is contained in the text stream, constantly flowing between the various components of the computer, continuously accepting the processing of the computer, and finally becoming a kind of service that the user needs.
(In other words, if you look at the hacker Empire, you will be impressed with the flow of text.) )
Standard input, standard output, standard error and redirection
When Linux executes a program, it automatically opens three streams, standard inputs, standard output, and standard error (STD). For example, when you open the command line, by default, the command line's standard input is connected to the keyboard, and standard output and standard errors are connected to the screen. For a program, although it always opens these three streams, it will be used as needed and not necessarily used.
Imagine hitting a
$ls
The text stream of the keystroke ("ls\n", \ n is the character entered when the carriage return, represents a newline) command line (the command line is actually a program). The command line then calls/bin/ls to get the result ("A.txt"), and finally the output text stream ("A.txt") flows to the screen, which is shown, for example:
A.txt
Assuming that we don't want the text stream to flow to the screen, but instead flow to another file, we can adopt a redirection (redirect) mechanism.
$ls > A.txt
REDIRECT Standard output. The > here is to remind the command line, let it know I want to change the direction of the text flow now, we do not let the standard output to the screen, but to a.txt this file (like rail track change). At this point, the computer creates a new A.txt file and points the standard output of the command line to the file.
There is another symbol:
$ls >> A.txt
The role of >> here is also to redirect the standard output. If a.txt already exists, the text stream generated by LS will be appended to the end of A.txt and will not create a new a.txt every time as >.
We introduce the command echo below:
$echo Iamvamei
Echo's role is to direct text flow to standard output. Here, the function of ECHO is to output the Iamvamei to the screen. If it is
$echo Iamvamei > A.txt
There will be Iamvamei this text in A.txt.
We can also use the < symbol to change the standard input. For example, the Cat command, which reads the text stream from the standard input and outputs it to the standard output:
$cat < A.txt
We point the cat standard input to a.txt and the text flows from the file to the cat and then to the screen. Of course, we can also redirect the standard output at the same time:
$cat < a.txt > B.txt
In this way, the contents of the a.txt are copied into the B.txt.
We can also use >& to redirect standard output and standard errors at the same time. Suppose we do not have a directory void. So
$CD void > A.txt
An error message is returned on the screen. Because the standard error still points to the screen at this time. When we use:
$CD void >& A.txt
Error messages are directed to A.txt.
If you only want to redirect standard errors, you can use 2>:
$CD void 2> a.txt > B.txt
Standard error corresponds to always 2nd number, so there is the above notation. Standard error output to a.txt, standard output output to b.txt.
Piping (pipe)
Once you understand the above, the plumbing concept is a breeze. Pipelines can direct the output of one command to the input of another, allowing two (or more commands) to work continuously like pipelining, continuously processing text streams. On the command line, we use | To represent the pipeline:
$cat < A.txt | Wc
The WC command represents word count, which is used to count the total number of lines, words, and characters in the text. The text in the A.txt flows first to cat, then from the standard output of cat to the standard input of the WC, allowing the WC to know that it is dealing with the a.txt string.
Linux commands are actually highly specialized and try to be independent of each other. Each one is focused on a small feature. But through the pipe, we can combine these functions together for some complex purposes.
Summarize
Text flow, standard input, standard output, standard error
Cat, Echo, WC
>>, <, |
Linux text stream