Linux Technology Learning: Detailed Explanation of Io Redirection Redirection

File descriptors are rarely used in bash. Starting from 0, the user indicates the ongoing data flow, 0 indicates standard input, 1 indicates standard output, 2 indicates error output, and others start from 3. The most common scenario is to output error messages to a file, you can add 2>file to our command.

Let's look at an example of a script below:

command > logfile 2>&1 &

  >logfile, indicating that the standard output of command is redirected to the file logfile, 2>&1, matching n>&m, indicating that the file description word 2 (command's standard error output) will be copied using the file description word 1 (ie standard output) , since the standard output has been redirected to logfile, this copy will also be redirected to the file lofgile. We can verify this effect with "abcd > logfile 2>&1 &". The last & indicates the way to run in the background. This command means that the command is run in the background, and its standard output and error output are redirected to the logfile file. A similar effect can be achieved as follows:

command 2>&1 | tee logfile &

Error output also applies to standard output, by means of pipe, see them as input to execute tee logfile. The tee command copies its standard input to its standard standard output and the file with its arguments. Unlike the above command, it will be output in both stdout and logfile at the same time.

Redirection of other file descriptors, such as <&n, is often used to read or write from multiple files.

<&- means to force close the standard input>&- means to force close the standard output 1>, equivalent to > 0<, equivalent to <linux shell data redirection (input redirection and output redirection) Before orientation, let's take a look at linux's file descriptors.

Linux file descriptor: It can be understood as a number allocated by Linux to track the open file. This number is somewhat similar to the handle of the C language when operating the file. The read and write operations of the file can be realized through the handle. The user can customize the range of file descriptors: 3-num. This maximum number is related to the number defined by the user: ulimit -n, and cannot exceed the maximum value.

After linux starts, three file descriptors will be opened by default, namely: standard input 0, correct output standard output 1, and error output: error output 2

After opening the file later. New file binding descriptors can be added sequentially. When a shell command is executed, it inherits the file descriptor of the parent process. Therefore, all shell commands run will have 3 file descriptors by default.

A command was executed:

There is an input first: the input can be obtained from the keyboard or from a file

Command execution completed: successful, the successful result will be output to the screen: standard output is the screen by default

There is an error in the execution of the command: the error will also be output to the screen: standard error also refers to the screen by default

File input and output are accomplished by tracking integer handles to all open files for a given process. These numeric values ​​are file descriptors. The most well-known file descriptors are stdin, stdout and stderr, and the numbers of file descriptors are 0, 1 and 2 respectively. These figures and respective equipment are reserved. Before a command is executed, all input and output will be prepared, and they are bound by default (stdin, stdout, stderr). If an error occurs at this time, the command will be terminated and will not be executed. For the command parsing process, please refer to: Introduction to Linux Shell Wildcards, Metacharacters, and Escapes

These default outputs and inputs are all set by the linux system. In the process of using, we sometimes do not want the execution results to be output to the screen. I want to output to a file or other device. At this time, we need to perform output redirection.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325218833&siteId=291194637