Best practices for Linux: Introduction to super-easy Linux System Management (serialization 5)

Source: Internet
Author: User
This article is the best practice of Linux. it aims to help Chinese cabbage understand how Linux processes interact. Several articles that were serialized previously have caused a lot of repercussions.

This article is the best practice of Linux. it aims to help Chinese cabbage understand how Linux processes interact. Several articles serialized previously caused a lot of repercussions. some people also reflected that there were few texts in the diagram, and they felt that there was no good news. this article selects most of the "pipelines and redirection" that are explained in actual practice for serialization, hoping that readers can truly learn something.

If you want to hear the voice broadcast from Niu every day and want to learn more IT technologies or skills that are not big enough in schools and books, please pay attention to the public account.:Xiniubook2008. Or addWeibo: I am the master of my books, Participate in the activity, get a book for free.

 

8.3 Pipelines and redirection

 

Pipelines and redirection are a communication method between processes in Linux and play an important role in system management. Most Linux processes require three file descriptors: standard input, standard output, and standard error output. the numbers are 0, 1, and 2. Generally, these three descriptors are associated with the terminal started by the process, and the input is generally the keyboard. Redirection and pipelines aim to redirect these descriptors. An MPs queue is usually a combination of input and output redirection. one process sends data to one end of the MPs queue, and the other process reads data from the other end of the MPs queue. The pipe character is "| ".

8.3.1 standard input and output

When you execute a Shell command, three standard files are automatically opened, as shown in Figure 8.3.

Figure 8.3 three standard files corresponding to Shell execution

The standard input file stdin usually corresponds to the terminal keyboard, the standard output file stdout and the standard error output file stderr. Both files correspond to the terminal screen. The process obtains the input data from the standard input file, outputs the normal output data to the standard output file, and prints the error information to the standard error file.

The following uses the cat command as an example to describe the standard input and output. The cat command is used to read data from the file provided by the command line and send the data directly to the standard output file. generally, it corresponds to the terminal screen, as shown in Example 8-7.

[Example 8-7]

[Root @ CentOS ~] # Cat/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE = eth0

HWADDR = 00: 0C: 29: 7F: 08: 9D

TYPE = Ethernet

UUID = 3268d86a-3245-4afa-94e0-f100a8efae44

ONBOOT = yes

BOOTPROTO = static

BROADCAST = 192.168.3.255

IPADDR = 192.168.3.100

NETMASK = 255.255.255.0

This command displays the contents of the file ifcfg-eth0 to the standard output, that is, the screen. If the cat command line does not contain parameters, data is generally read from the corresponding keyboard in the standard input file and sent to the standard output file, as shown in Example 8-8.

[Example 8-8]

# When cat does not contain any parameters, it reads data from the standard input and displays the data in the standard output file.

[Root @ CentOS ~] # Cat

Mycontent

Mycontent

Hello

Hello

Each line of information entered by the user is immediately output to the screen by the cat command. The user cannot process the input data further. To solve this problem, the Linux operating system introduces two other mechanisms for input and output transmission: input/output redirection and pipeline.

8.3.2 input redirection

Input redirection refers to redirecting standard input from a command or executable program to a specified file. That is, the input can come from a specified file instead of the keyboard. Input redirection is mainly used to change the input source of a command.

For example, the cat command in the previous example does not respond after you type the command. all text entered from the keyboard appears on the screen until you press Ctrl + d, the command will terminate. two methods can be used: one is to give a file name for the command, and the other is to use input redirection.

The general format of input redirection is: Command <文件名,输入重定向符号为“<”。示例8-9演示了此种情况,此示例中的文件已不是参数,而是标准输入。< p>

[Example 8-9]

[Root @ CentOS ~] # Cat </etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE = eth0

HWADDR = 00: 0C: 29: 7F: 08: 9D

TYPE = Ethernet

UUID = 3268d86a-3245-4afa-94e0-f100a8efae44

ONBOOT = yes

BOOTPROTO = static

BROADCAST = 192.168.3.255

IPADDR = 192.168.3.100

NETMASK = 255.255.255.0

[Root @ CentOS ~] # Wc

99 188

There is also an input redirection, as shown in Example 8-10.

[Example 8-10]

[Root @ CentOS ~] # Cat <

> Line1

> Line2

> Line3

> EEE

Line1

Line2

Line3

The identifier "EEE" indicates the start and end delimiters of the input. this name is not fixed and can be separated by other strings. The redirection operator of the document is "<". Redirects the text between a pair of delimiters to the input command. For example, in the above example, the content between "EEE" is used as the body and then as the input to pass to the cat command. Most commands specify the input file name in the command line in the form of parameters, so input redirection is not often used. Some commands that cannot use file names as input parameters can be used. if the required input content is in another file, you can use input redirection to solve the problem.

8.3.3 output redirection

Output redirection refers to redirecting standard output or standard error output of commands or executable programs to a specified file. The command output is not displayed on the screen, but written to the specified file for future troubleshooting or other purposes. Output redirection is more commonly used than input redirection, which can be used in many cases. For example, if a command has a lot of output and cannot be fully displayed on the screen, redirect the output to a file and open the file with a text editor to view the output information, you can use this method to save the output of a command. Also, output redirection can be used to input a command as another command. Another simpler method is to use pipelines, which will be described in the following section.

The general format of output redirection is: Command> file name, that is, the output redirection symbol is ">", as shown in Example 8-11.

[Example 8-11]

# Redirecting output to a file

[Root @ CentOS ~] # Ls-l/> dir.txt

[Root @ CentOS ~] # Head-n5 dir.txt

Total 114

Dr-xr-xr-x.2 root root4096 Jun8 00:54 bin

Dr-xr-xr-x.5 root root1024 Apr 13 boot

Dr-xr-xr-x.7 root root4096 Mar6 cdrom

Drwxr-xr-x.18 root root4096 Jun8 0:07 data

Use the "ls-lonics command to display the current directory and file, and output the result to the dir.txt file in the current directory instead of on the screen. You can use the cat command for the content of the nvidir.txt file. note whether the result is the same as that displayed when you directly use the "ls-l" command.

Note: If the file after the ">" symbol already exists, the file will be overwritten.

To avoid overwriting the specified file content in the output redirection command, Shell provides an append method for output redirection. The output append redirection function is similar to the output redirection function. The difference is that the output result of the command or executable program is appended to the end of the specified file, in this case, the original content of the file is not overwritten. Append the redirection operator ">" in the format of command> file name, as shown in Example 8-12.

[Example 8-12]

# Use redirection to append file content

[Root @ CentOS ~] # Ls-l/usr> dir.txt

The output of the above command will be appended to the last position of the file, and the original content will not be overwritten.

8.3.4 error output redirection

Like the standard output redirection of a program, the program's error output can also be reoriented. You can use the symbol "2>" or append the symbol "2>" to redirect error output. To print any program error information to a file for troubleshooting, use the method in Example 8-13.

[Example 8-13]

# If the file does not exist, a standard error is generated, which is usually displayed on the screen.

[Root @ CentOS ~] # Ls/xxxx

Ls: cannot access/xxxx: No such file or directory

#1 indicates the redirection standard output, but is it not an error output? the output is still printed to the screen.

[Root @ CentOS ~] # Ls/xxxx 1> stdout

Ls: cannot access/xxxx: No such file or directory

# REDIRECT standard output and standard error output respectively

[Root @ CentOS ~] # Ls/xxxx 1> stdout 2> stderr

# View the file content, consistent with the result printed to the screen

[Root @ CentOS ~] # Cat stderr

Ls: cannot access/xxxx: No such file or directory

# Direct both standard output and standard error output to standard output files

[Root @ CentOS ~] # Ls/xxxx 1> stdout 2> & 1

[Root @ CentOS ~] # Cat stdout

Ls: cannot access/xxxx: No such file or directory

# Another redirection syntax

[Root @ CentOS ~] # Ls/xxxxx &> stderr

[Root @ CentOS ~] # Ls/xxxxx/&> stdout

# View output file content

[Root @ CentOS ~] # Head stdout

Ls: cannot access/xxxxx: No such file or directory

/:

Bin

Boot

Cdrom

The/xxxx directory does not exist, so there is no standard output, only error output. The preceding example demonstrates the error Output Content. when the standard output is redirected, the standard output is not redirected, so the error output is printed to the screen. Use "2> stderr" to locate the error output to the specified file, and redirect the standard error output to the standard output. after the output is executed, no content is displayed on the screen, run the cat command to view the file content and see the error message of the above command. You can also use another output redirection operator "&>" to send standard output and error output to the same file. Table 8-3 lists common input/output redirection methods.

Table 8.3 common redirection definitions

Parameters

Description

Command> filename

Redirects standard output to a file

Command> filename

Redirects the standard output to a file.

Command 1> fielname

Redirects standard output to a file

Command> filename 2> & 1

Redirects the standard output and standard error output to a file.

Command 2> filename

Redirects the standard error output to a file.

Command <filename> filename2

Input with filename as the standard and output with filename2 as the standard

Command <filename

Use filename as the standard command input.

Command <delimiter

Reads data from standard input until delimiter is encountered.

8.3.5 MPs queue

There are two ways to use the output of one program or command as the input of another program or command: one is to combine two commands or programs through a temporary file; another method is to use pipelines.

The pipeline can connect a series of commands. the output of the preceding command can be used as the input of subsequent commands. the output of 1st commands can be sent to 2nd commands using the pipeline, the output of 2nd commands is used as the input of 3rd commands, and so on. If no output redirection is used in the command line, the output of the last command or other command execution exceptions in the pipeline line are displayed on the screen. Use the pipe character "|" to create a pipe line, as shown in Example 8-14.

[Example 8-14]

[Root @ CentOS ~] # Cat/etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADD

IPADDR = 192.168.3.100

# Post-pipeline

[Root @ CentOS ~] # Cat/etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADD | awk-F = '{print $2 }'

192.168.3.100

The content output by the cat command in the preceding example is sent to the grep command in the form of a pipe, and then the file content is searched through string matching.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.