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

Example Analysis of io Redirection in Linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you the content of a sample analysis of io redirection in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Linux io redirection is to redirect the data information that is supposed to be output to the screen to a specified file or to a black hole (/ dev/null).

Standard input and output

When a process manipulates a file

First of all, the process cannot directly access the hardware and needs the help of the kernel to access the file.

The kernel kernel needs to use the file descriptor (file descriptor) to access files.

Summary: process-through-> file descriptor (non-negative integer)-access-> file name, process uses file description to manage open file correspondence

Step 1: run tail-f / etc/passwd

Step 2: use the file descriptor 3-> / etc/passwd- > inode- > block

Step 3: correct output-> by default, through file description 1-> current terminal 1 > (change the location of the output pair, file or black hole)

Step 4: error output-> by default through file description 2-> current terminal 2 >

Usually, when a program accesses a file, it opens at least three standard files, namely, standard input, standard output and error output.

The process will get the data from the standard input, print the normal output to the screen terminal, and print the wrong output information to the screen terminal.

Standard input (STDIN) file descriptor 0 function: the default is the keyboard, or it can be the output of a file or other command

Standard output (STDOUT) file descriptor 1 function: default output to the screen

Error output (STDERR) file descriptor 2 function: default output to the screen

File name (filename) file descriptor 3 +

Output redirection

The output is redirected to change the location of the output. There are several ways to redirect output, as follows

Standard override output redirection operator: 1 > purpose: output the correct result of the program output to the specified file, overwriting the original contents of the file

Standard append output redirection operator: 1 > > purpose: output the correct result of the program output to the specified file in an appended way, without overwriting the original file

Error overwrite output redirection operator: 2 > purpose: output the error result of the program to the executed file, which will overwrite the original contents of the file.

Error append output redirection operator:: 2 > purpose: output the error result of the program output to the specified file in an appended way, and will not overwrite the original file

Case 1-Standard output redirection

Standard output redirection example

1. Create [root@node: ~] # > edu.txt if the file does not exist

two。 Clear the content [root@node: ~] # ifconfig ens32 > edu.txt if the file exists

Case 2-Standard additional output redirection

1. Create 2. 0 if the file does not exist. If the file exists, add content at the end of the file

[root@node: ~] # echo "Hello Students" > > if

Case 3-error output redirection standard error output redirection

1. Correct output and error output to the same file

[mg@node ~] $find / etc-name "* .conf" 1 > ok 2 > ok

two。 Correct output and error output to different files

[mg@node ~] $find / etc-name "* .conf" 1 > ok1 2 > err

Mixed output redirection

1. Mix the correct output error output to the same file [mg@node ~] $find / etc-name "* .conf" & > ab

two。 Combine the contents of two files into one file [mg@node ~] $cat ab > c

Both correct and wrong output to the same location [root@node: ~] # ls / root/error > ab 2 > & 1

Output content to a black hole

Output content to black hole device / dev/null

[root@node: ~] # ls / root / error > ab 2 > / dev/null

[root@node: ~] # ls / root / error > ab & > / dev/null

Enter a redirect case

Input redirection: refers to "redesigning the device" to "replace the keyboard" as a new input device

Input redirection example 1. Read the contents of the file [root@node: ~] # cat by entering redirection

two. Read multiple lines of content [root@node: ~] # cat by entering redirection

3. Import the redirected incense data into the database by input

[root@node: ~] # mysql-uroot-poldxu.com

Script input redirection

Use input redirection to print the menu navigation bar for the installation service

Enter redirect scene 1. When recovering data, 2. The script prints the menu using 3.cat when writing to the file, vim

Output redirection correct output: 1 >: overwrite (clear first, then write) 1 > >: write directly to the tail (end) of the file amount

Error output: 2 >: overwrite (clear first, then write) 2 > >: directly westward into the end of the file (end)

Mixed output & > / dev/null & > > / dev/null 1 > / devnull 2 > & 1

Process pipeline technology what is the pipeline operation symbol "|", which is mainly used to connect the left and right commands and give the [standard output] of the left command to the [standard input] of the right command.

Note: standard error output cannot be passed to the latter command

The pipeline command character allows users to further master the collocation and use of commands, and further improve the efficiency of command output.

Pipeline flow schematic format: cmd1 | cmd2 | [… | | cmdn] Pipeline use case |

Sort the users in / etc/passwd by UD size [root@node: ~] # sort-t ":"-K3-n / etc/passwd | head

Tee and xargs

Using tee in a pipe is equivalent to opening an opening in the middle, intercepting the contents of the first command and saving it to a file, but should not affect the output of the next command

# option:-an append [root@node: ~] # ip addr | grep "inet" | tee ip1.txt | awk-F'/ 'print $1}' awk'{prnt $2'

Cat ip.txt [root@node: ~] # cat ip.txt inet 127.0.0.1 scope host 8 scope host lo inet6:: 1max 128

Xargs xargs parameter transfer is used in the pipeline, which mainly allows some unsupported pipeline commands to use pipeline technology.

[root@node: ~] # which cat | xargs ls-l-rwxr-xr-x. 1 root root 54080 Nov 17 2020 / usr/bin/cat

Ls | xargs rm-fv deletes all files in the current directory

Thank you for reading! This is the end of this article on "sample analysis of io redirection in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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