Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
File for data storageIt is equivalent to a house that stores data. We previously said that the so-called data is a sequence of 0 or 1, but strictly speaking, Linux usesBytes(Byte) is used as the unit of data. That is to say, every eight bits of this sequence is a unit (the decimal range of the eight bits is 0 to 255 ). Using ASCII encoding, you can convert such a byte into a character. Therefore, in Linux, the data we call can be expressed in characters, that is, text.
In fact, if characters are processed in BIT units, the machine will be easier to read and transmit, and the efficiency will be higher. But why does Linux still process data in bytes? The reason is that, compared to processing data in bits, data can be easily convertedCharacter. Compared with the boring 0 and 1, it is easier to use characters.Read by people(Human readable). However, not all data is designed to be understandable, for example, the various characters contained in an executable file are meaningless to humans (because the executable file is intended for the machine to understand ). However, Linux still processes all files in bytes, so that all files canShare a set of interfaces(Virtual File System) to reduce the complexity of Linux design.
("Everything is a file" is one of the common UNIX design philosophies, but Linus corrected this and changed it to "Everything is a stream of bytes ".)
However, data is not settled forever after a file is found. It is often read into memory (like working in the office), or transmitted to external devices (like taking a vacation at a hotel), or moved to another house. In such a relocation process, data is like a group of people walking, we call itText Stream(Text Stream, Or byte stream ). However, the connection methods between different devices in a computer vary greatly. The connection from memory to files is like a mountain hill, and from memory to peripherals is like traveling through a river. Therefore, Linux also definesStream)To build connections between different locations.Highway. The benefit of stream is that, whether you are from memory to peripherals, or from memory to files, all roads are the same (you don't have to worry about stone or land under the road ).
Let's repeat the phrase "Everything is a stream of bytes. Information is contained in the text stream, constantly flowing between various components of the computer, constantly accepting computer processing, and finally becoming a service that the user needs.
(If you have read the hacker empire, you will surely be impressed with the text stream .)
2. Standard input, standard output, standard errors and redirection
When Linux executesProgramThree streams are automatically opened,Standard Input(Standard input ),Standard output(Standard output ),Standard Error(Standard error ). For example, when you open the command line, the standard input of the command line is connected to the keyboard by default, and both the standard output and standard error are connected to the screen. For a program, although it will always open these three streams, it will be used as needed, not necessarily.
Imagine hitting
$ Ls
The text stream ("ls \ n", \ n is the character entered when you press enter, indicating line feed) Command Line (the command line is actually a program ). The command line then calls/bin/ls to obtain the result ("a.txt"). The output text stream ("a.txt") is then sent to the screen and displayed, for example:
A.txt
Suppose we don't want to let the text stream flow to the screen, but to another file, we can useRedirect(Redirect) mechanism.
$ Ls> a.txt
Retargeting standard output. Here>I want to change the direction of the train now. Instead, we want to output the standard output to the screen, but to the.txt file (like a train track change ). Then, a new a.txt file is created, and the standard output of the command line is directed to this file.
There is another symbol:
$ Ls> a.txt
Here>Is also usedRetargeting standard output. If a.txt already exists, the lssag will be appended to the end of a.txt, rather than creating a.txt every time.
The following describes the echo command:
$ Echo iamvamei
EchoThe role isExport text streams to standard output. Here, ECHO is used to output iamvamei to the screen. If yes
$ Echo iamvamei> a.txt
A.txt contains the iamvamei text.
We can also use<SymbolChange standard input. For exampleCatCommand, it canRead text streams from standard input,And output to standard output:
$ Cat <a.txt
We direct the standard catinput to a.txt. The text will flow from the file to CAT and then output to the screen. Of course, we can also redirect the standard output at the same time:
$ Cat <a.txt> B .txt
In this example, the.txt content is copied to B .txt.
We can also use & gt;Retargeting standard output and standard errors at the same time. Suppose we do not have a directory void. So
$ CD void> a.txt
Will return an error message on the screen. This is because the standard error still points to the screen. When we use:
$ CD void> & a.txt
The error message is directed to a.txt.
If you only wantRedirection standard error, You can use2>:
$ CD void 2> a.txt> B .txt
The standard error always corresponds to the 2nd, so the above code is used. The standard output is a.txt by mistake, and the standard output is B .txt.
3. Pipeline (PIPE)
After understanding the above content,MPs queueThe concept is easy. The pipeline can direct the output of one command to the input of another command, so that two (or more) commands can work continuously as the pipeline and process text streams. In the command line, we use|Pipeline:
$ Cat <a.txt | WC
WCCommand represents word count, usedStatisticsTextLine,WordAndCharacter. Token string.
Linux commands are highly specialized and as independent as possible. Each one focuses only on a small feature. However, through pipe, we can combine these functions to achieve some complex purposes.
Summary:
Text Stream, standard input, standard output, standard error
Cat, Echo, WC
>,>>,<, |