Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux text stream

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Text has been modified previously with a text editor. Now, we need to understand the so-called "text."

text stream

In computers, data is a binary sequence of 0s and 1s, but strictly speaking Unix uses bytes as units of data, meaning that the sequence is one bit at a time. An eight-digit binary number that falls in the decimal range 0 to 255. Using ASCII encoding, this byte can be converted into one of 256 characters. So, in Unix, data can be expressed in character form, which is called text.

In fact, machines would be easier to read and transmit if bits were used. But Unix systems insist on representing data in bytes. The reason is that text in bytes is directly human readable, as opposed to binary data in bits. In this way, whether it is computer configuration information or a poem written by someone else, users can read it directly. Of course, not all data is designed to be read. Many compiled executable files contain content that only machines can read. Open this file, although you can also see a character, but these characters do not make any meaningful text. But Unix systems don't backdoor such "unreadable" files. All files are in a uniform form, so they can be stored in the same way, and they can share a set of processing tools, thus reducing the difficulty of program development.

Files that store text are equivalent to houses that store data. In the Unix design philosophy, there has always been a saying that "Everything is a file." Most of the files correspond to the information on the storage device, which is the SD card of the Raspberry Pi. Even the directory, which represents the location of files, is a file. In addition, the program configuration information is also stored in the file. For Unix systems, files can be broadly considered objects that can provide or receive data. In this case, Unix systems simply represent the hardware that provides or receives data as files. Among them, there are both externally connected USB devices and hardware such as memory inside the Raspberry Pi. In the/dev directory, you can find many of these files representing hardware.

But Torvalds corrected "Everything is a file" by saying "Everything is a stream of bytes." When the system is running, the data is not residing in a file. Data will flow constantly under the command of the CPU, just like a hard-working office worker. Sometimes the data needs to go to the office for work, so it is read into memory, and sometimes it goes to the hotel for vacation and is transmitted to external devices. Sometimes, data needs to be moved to another home and transferred to another file. As it runs around, the data looks like a stream of people walking in a queue. We call it a text stream, or byte stream. However, the way computers connect to each other varies widely, from memory to files like a mountain climb to memory to peripherals like swimming across a river. To this end, Unix defines streams as highway standards for connecting operating systems everywhere. With streaming, all data highways are formatted the same, whether from memory to peripherals or from memory to files. As for whether the road is stone or land, it is left to the operating system to deal with, without worrying about the user.

(As an aside, if you've ever seen The Matrix, you'll be impressed by text flow.)

Standard input, standard output, standard error and redirection

When Unix 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, standard input of the command line is connected to the keyboard, and standard output and standard error are connected to the screen. For a program, although it will always open these three streams, it will use them as needed, not necessarily.

Imagine hitting a button

$ls

The text stream of keyboard strokes ("ls\n,"\n is the character entered at carriage return, indicating line feed) 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 is displayed, for example:

a.txt

Suppose we don't want text to flow to the screen, but to another file. We can use the redirection mechanism.

$ls > a.txt

Redirects standard output. > here is to remind the command line, let it know that I now want to change the direction of the text flow, we do not let the standard output to the screen, but to the a.txt file (like a train track change). At this point, the computer creates a new a.txt file and directs the standard output of the command line to this file.

There's another symbol:

$ls >> a.txt

Here>> also redirects 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 as in>.

We introduce the command echo below:

$echo IamVamei

Echo directs the text stream to standard output. Here, echo is used to output IamVamei to the screen. if it is

$echo IamVamei > a.txt

A.txt will contain the text IamVamei.

We can also use b.txt

The contents of a.txt are copied into b.txt.

We can also use>& to redirect both standard output and standard error. Suppose we don't have a directory void. so

$cd void > a.txt

An error message is returned on the screen. Because standard error is still pointing to the screen. 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

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

pipe

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

$cat

< a.txt | wc wc命令代表word count,用于统计文本中的行、词以及字符的总数。a.txt中的文本先流到cat,然后从cat的标准输出流到wc的标准输入,从而让wc知道自己要处理的是a.txt这个字符串。 Linux的各个命令实际上高度专业化,并尽量相互独立。每一个都只专注于一个小的功能。但通过pipe,我们可以将这些功能合在一起,实现一些复杂的目的。 总结 文本流,标准输入,标准输出,标准错误 cat, echo, wc >

, >>,

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report