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

How to redirect input and output in Linux

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to redirect input and output in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Input redirection: refers to the reassignment of the device to replace the keyboard as the new input device; output redirection refers to the reassignment of the device to replace the monitor as the new output device. The result of the execution of a file or command is usually used instead of the keyboard as a new input device, while a new output device usually refers to a file

Linux input redirection requires symbols and functions for input redirection as shown in Table 1.

The command symbol format function command reads the specified file as the input device command representation of the command from the standard input device (keyboard) and does not stop until it encounters a delimiter (the data read does not include the delimiter). The delimiter here is actually a custom string command file 2 that takes file 1 as the input device of the command, and the execution result of the command is output to file 2. [example 1] by default, the cat command accepts input from a standard input device (keyboard) and displays it to the console, but if a file is used instead of the keyboard as an input device, the command takes the specified file as the input device and reads and displays the contents of the file to the console. Take the / etc/passwd file, which stores the basic information of all users in the system, as an example, execute the following command:

[root@localhost ~] # cat / etc/passwd # omits the output information here, and readers can check the output result of [root@localhost ~] # cat # the same as the above command. Although the execution result is the same, the first line represents the keyboard as the input device, while the second line of code uses the / etc/passwd file as the input device.

[example 2]

[root@localhost ~] # cat > c.biancheng.net > Linux > 0 c.biancheng.net Linux you can see that when 0 is specified as the delimiter, you can enter data all the time as long as you don't enter 0.

[example 3] first, create a new text file a.tx, and then execute the following command:

[root@localhost ~] # cat a.txt [root@localhost ~] # cat a.txt [root@localhost ~] # cat a.txt # outputs the same data as the / etc/passwd file. By redirecting / etc/passwd as the input device and redirecting the output to a.txt, you can finally copy the contents of the / etc/passwd file to a.txt.

Linux output redirection uses output redirection more frequently than input redirection. And, unlike input redirection, output redirection can be subdivided into standard output redirection and error output redirection. For example, use the ls command to view the attribute information of two files separately, but one of the files does not exist, as follows:

[root@localhost ~] # touch demo1.txt [root@localhost ~] # ls-l demo1.txt-rw-rw-r--. 1 root root 0 Oct 12 15:02 demo1.txt [root@localhost ~] # ls-l demo2.txt ls: cannot access demo2.txt: No such file or directory in the above command, demo1.txt exists, so some attribute information of the file is correctly output, which is also the standard output information of the command execution; while demo2.txt does not exist, so the error message displayed after executing the ls command is the error output information of the command.

Again, if you want to write the data that was originally output to the screen to a file, the two kinds of output information should be treated differently. On this basis, standard output redirection and error output redirection include empty write mode and append write mode respectively. Therefore, the symbols and functions that are required for output redirection are shown in Table 2.

The command symbol format functions the command > file redirects the standard output of the command execution to the specified file, and if the file already contains data, the original data is cleared and the new data is written. The command 2 > file redirects the error output of the command execution to the specified file, and if the file already contains data, the original data is cleared and the new data is written. The command > > file redirects the standard output of the command execution to the specified file, and if the file already contains data, the new data will be written to the end of the original content. The command 2 > > file redirects the error output of the command execution to the specified file, and if the file already contains data, the new data is written to the end of the original content. Command > > File 2 > & 1 or Command & > > File writes standard output or error output to the specified file, and if the file already contains data, the new data is written to the end of the original content. Note that in the first format, the last 2 > & 1 is integrated and can be considered a fixed way of writing. The above is how to redirect the input and output in the Linux shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report