Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3.1 Input and output redirection


May 23, 2021 That's what Linux should learn



Now that we've learned almost all the basic and commonly used Linux commands in the last chapter, the next task is to put multiple Linux commands together appropriately so that they work together so that we can work with the data more efficiently. To do this, you must understand the principles of input redirection and output redirection of commands.

In short, input redirection refers to importing a file into a command, while output redirection refers to writing data information that was intended to be output to the screen to the specified file. I n our daily learning and work, we use output redirects more frequently than input redirects, so we subdite output redirects into two different techniques: standard output redirection and error output redirection, as well as empty writes and append writes. S ounds mysterious? Mr. Liu Wei will go slowly next.

Standard input redirection (STDIN, file descriptor 0): Entered by default from the keyboard or from other files or commands.

Standard output redirection (STDOUT, file descriptor 1): The default output to the screen.

Error output redirection (STDERR, file descriptor 2): The default output to the screen.

For example, we look at the properties of two files, the second file does not exist, although the operation of both files will be on the screen to output some data information, but the difference between the two operations is actually very large:

[root@linuxprobe ~]# touch linuxprobe [root@linuxprobe ~]# ls -l linuxprobe -rw-r--r--. 1 root root 0 Aug 5 05:35 linuxprobe [root@linuxprobe ~]# ls -l xxxxxx ls: cannot access xxxxxx: No such file or directory

In the above command, a file named linuxprobe exists, and the output information is some of the relevant permissions, owners, groups, file size, and modification time of the file, which is also the standard output information for the command. A second file named xxxxxx does not exist, so the error message displayed after the ls command is executed is also the error output of the command. The two outputs need to be treated differently if you want to write the data that was originally output to the screen into a file instead.

For input redirects, the symbols used and their effects are shown in Table 3-1.

Table 3-1 enters the symbols used in the redirect and their effects

Symbolic Action Commands and Files The file is read in from the standard input by the command's standard input, and the demarcation character is not stopped until the demarcation is met The command is stopped, and file 1 is used as the standard input for the command and the standard output to file 2 is used for output redirection, as shown in Table 3-2.

Table 3-2 outputs the symbols used in the redirection and their effects

Symbols act

Command and File Redirect standard output to a file (empty the data from the original file) Command 2 and File Redirect the error output to a file (empty the data from the original file) Command and file Redirect the standard output to a file (appended to the back of the original content) Command 2 and file to redirect the error output to a file (redirect to the data of the original content later). The standard output is written to the file together with the error output (appended to the back of the original content)

For the standard output mode in the redirect, you can omit file descriptor 1 from writing, while file descriptor 2 in the error output mode must be written. L et's try the cow knife first. T he information that the man bash command was intended to output to the screen is written to the file readme .txt by standard output redirection, and then the contents of the .txt readme are displayed. The specific commands are as follows:

[root@linuxprobe ~]# man bash > readme.txt [root@linuxprobe ~]# cat readme.txt BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again SHell

SYNOPSIS bash [options] [file]

COPYRIGHT Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.

DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incor‐ porates useful features from the Korn and C shells (ksh and csh).

Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). B ash can be configured to be POSIX-conformant by default. . ................. O mit some of the output information... D o you feel very convenient? L et's next try the changes brought about by the two different modes of overlay write and append write in output redirection technology. First write a row of data to the readme.txt file by overwriting write mode (which contains the man command information from the last experiment), and then write the data to the file again by appending write mode, the commands are as follows:

[root@linuxprobe ~]# echo "Welcome to LinuxProbe.Com" > readme.txt [root@linuxprobe ~]# echo "Quality linux learning materials" >> readme.txt

After executing the cat command, you can see the contents of the file as follows:

[root@linuxprobe ~]# cat readme.txt Welcome to LinuxProbe.Com Quality linux learning materials

Although both output redirection techniques are technology, there is a difference between the standard and error outputs of different commands. F or example, look at information about a file in the current directory, in this case a linuxprobe file. Because the file is real, standard output allows you to write information that was intended to be output to the screen to the file, while incorrect output redirects still output the information to the screen.

[root@linuxprobe ~]# ls -l linuxprobe -rw-r--r--. 1 root root 0 Mar 1 13:30 linuxprobe [root@linuxprobe ~]# ls -l linuxprobe > /root/stderr.txt [root@linuxprobe ~]# ls -l linuxprobe 2> /root/stderr.txt -rw-r--r--. 1 root root 0 Mar 1 13:30 linuxprobe

What if you want to write the error information of the command to a file? T his is especially useful when the user is executing an automated Shell script, because it records the error information throughout the script execution process in a file, making it easy to drain the error after installation. Let's then experiment with a file that doesn't exist:

[root@linuxprobe ~]# ls -l xxxxxx cannot access xxxxxx: No such file or directory [root@linuxprobe ~]# ls -l xxxxxx > /root/stderr.txt cannot access xxxxxx: No such file or directory [root@linuxprobe ~]# ls -l xxxxxx 2> /root/stderr.txt [root@linuxprobe ~]# cat /root/stderr.txt ls: cannot access xxxxxx: No such file or directory

Input redirects are relatively lukewarm and less likely to be encountered at work. T he role of input redirection is to import files directly into the command. Next, use input redirects to import the .txt readme to the wc-l command to count the number of lines of content in the file.

[root@linuxprobe ~]# wc -l < readme.txt 2

The above command is actually equivalent to the cat readme .txt | The pipeline command combination of wc -l.