Deep understanding of Linux text stream

  

I believe many people know what system files are, but few people know what the text stream is. In fact, the text stream is not difficult to understand. The following small series will give you a detailed introduction to the Linux text stream. Get up and study.

Text Streams

Files are used for data storage, which is equivalent to a house that stores data. We said before that the so-called data is a sequence of 0 or 1, but strictly speaking, Linux uses bytes as the unit of data, that is, the sequence is one unit per eight bits (eight bits). The corresponding decimal range is 0 to 255). Using ASCII encoding, you can convert such a byte into a character. Therefore, in Linux, the data we are talking about can be expressed in characters, that is, in the form of text.

In fact, if you process characters in bits, the machine will be easier to read and transfer, and the efficiency will be higher. But why is Linux still processing in bytes? The reason is that it is easier to convert data into characters in units of bytes, as opposed to processing data in units of bits. Characters are more easily human readable than boring 0 and 1. However, not all data is designed to be readable, for example, the various characters contained in the executable do not make sense to people (because the executable is for the machine to understand). But Linux still processes all files in bytes, in order to allow all files to share a virtual file system, thus reducing the complexity of Linux design.

(&yquo;everything is a file” is one of the philosophies of UNIX design that is usually circulated, but Linus corrects this and changes to "everything is a stream of bytes".)

However, the data is not settled forever after finding its own file. It is often read into memory (like going to work in the office), or being sent to an external device (like going to a hotel vacation), or moving to another house. In such a relocation process, the data is like a stream of people walking in a row, we call it a text stream (or byte stream). However, the connection methods between different devices on a computer vary greatly. The connection from memory to file is like climbing a mountain. From memory to peripherals, it seems to swim across a river. To this end, Linux also defines streams as a standard for building roads that connect to each other. The advantage of Stream is that all roads are the same whether you are from memory to peripherals, or from memory to files (as for stones or land below the road, you don't have to worry about it).

Let's revisit the phrase "everything is a stream of bytes". Information is contained in the text stream, constantly flowing between the various components of the computer, constantly accepting the processing of the computer, and ultimately becoming a service that the user needs.

(As an aside, if you've seen The Matrix, it will surely be impressed by the text stream.)

standard input, standard output, standard error and redirect < Br>

When Linux executes a program, it automatically opens three streams, standard input, standard output, and standard error. For example, when you open the command line, by default, the standard input of the command line is connected to the keyboard, and both 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 used.

Imagine typing a

code as follows:

$ls

Text flow from keyboard strokes (“ls\ ”,\ Is the character entered when returning, indicating 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 and displays it, for example:

As follows:

a.txt

Suppose we don't want the text stream to flow to the screen, but to another file, we can use a redirect mechanism.

The code is as follows:

$ls 》 a.txt

Redirect the standard output. Here is to remind the command line, let it know that I want to change the direction of the text stream now, we do not let the standard output output to the screen, but to the a.txt file (as if the train track is changed). At this point, the computer will create a new a.txt file and point the standard output of the command line to this file.

There is another symbol:

The code is as follows:

$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, instead of creating a.txt every time like "."

We will introduce the command echo:

The code is as follows:

$echo IamVamei

The purpose of echo is to direct the text stream to standard output. Here, the role of echo is to output IamVamei to the screen. If the

code is as follows:

$echo IamVamei 》 a.txt

A.txt will have the text IamVamei.

We can also use the "symbol to change the standard input." For example, the cat command can read the text stream from standard input and output it to standard output:

The code is as follows:

$cat " a.txt

We will cat The standard input points to a.txt, and the text flows from the file to cat and then to the screen. Of course, we can also redirect the standard output at the same time:

The code is as follows:

$cat "a.txt" b.txt

Thus, the content of a.txt is Copied to b.txt.

We can also use "&" to redirect standard output and standard errors at the same time. Suppose we don't have a directory void. Then the

code is as follows:

$cd void 》 a.txt

will return an error message on the screen. Because the standard error still points to the screen at this time. When we use:

The code is as follows:

$cd void 》& a.txt

The error message is directed to a.txt.

If you only want to redirect standard errors, you can use 2::

The code is as follows:

$cd void 2 a.txt 》 b.txt

The standard error is always the number 2, so there is the above. The standard error is output to a.txt and the standard output is output to b.txt.

Pipes

After understanding the above, the concept of a pipeline is easy. A pipe can direct the output of one command to the input of another command, allowing two (or more commands) to work continuously like a pipeline, constantly processing the text stream. In the command line, we use | Represents the pipeline:

The code is as follows:

$cat " a.txt |  Wc

The wc command represents word count and is used to count the total number of lines, words, and characters in the text. The text in a.txt flows to cat first, then from the standard output of cat to the standard input of wc, so that wc knows that it is dealing with the string a.txt.

Linux commands are actually highly specialized and as independent as possible. Each one focuses on only one small function. But with pipe, we can combine these functions for some complex purposes.

Summary

Text Flow, Standard Input, Standard Output, Standard Error

cat, echo, wc

》, 》》, 《, |

The above is a detailed introduction of the Linux text stream. This article mainly introduces standard input, standard output, standard error and redirection. So, do you have a certain understanding of Linux text flow?

Copyright © Windows knowledge All Rights Reserved