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

Detailed introduction of file descriptors in Bash

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

Linux treats all kernel objects as files, and the system uses a size_t type to represent a file object. For example, the file descriptor 0 represents the system's standard input device STDIN. Usually, the value of STDIN is the keyboard. For example, the read command reads data from STDIN by default. Of course, the value of STDIN can be changed, such as changing it to other files. In this way, commands such as read will read data from the corresponding file by default.

To put it simply, a file descriptor can be hooked to a file. Once hooked, you can get the handle to the file by taking the address operator, for example, & 0 can get the handle of the STDIN device in memory (the device is also treated as a file in the system). It can be understood that if it is an ordinary variable var in shell, you can get the value represented by the variable in the form of $var. For a file descriptor fd, the handle to the file pointed to by the file descriptor can be obtained in the form of & fd, and this handle can be simply understood as the path to the file.

Redirect operation is often used in Shell programming, which essentially operates on file descriptors. This article will give a detailed introduction to the file descriptors in Shell scripts.

Default standard file descriptor

There are three standard file descriptors by default when each process starts:

Stdin 0 descriptor, which represents the input device, from which the process reads data; stdout 1 descriptor, to which the process writes data; and stderr 2 descriptor, to which the process writes error messages

By default, these three descriptors correspond to the same tty device, so that we can enter data and obtain the output of the process in the terminal.

The default file descriptor can also be replaced, for example, we can replace stdout to a file, so that the output of the command is not printed to the terminal, but is output to the file:

In the above demo, we first point the stdout to the file / tmp/stdout through exec 1 > / tmp/stdout, and then we execute two commands ls and pwd. We can see that the terminal has no command output at this time. When we restore stdout through exec 1 > & 2, we can find that the output of the previous command is stored in the file / tmp/stdout.

Exec is a bash built-in command, which is different from fork a child process when executing the command in the terminal. The command executed through exec will directly modify the current shell process, and you can use it to execute the command to modify the context of the current shell.

If you want to be bad, you can add exec 1 > / tmp/stdout to someone else's bashrc, so you can't see the output of the command in all the newly opened terminal windows, and you are not responsible for being beaten.

Operation of file descriptor

The operation on file descriptors in Shell consists of three parts: (Left, Operation, Right):

Left can be a number from 0 to 9, representing the file descriptor n

Left can also be &, indicating that the simultaneous operation of stdout and stderrRight can be a file name or a number from 0 to 9. When Right is a number, a & symbol must be added to indicate a reference to the n file descriptor.

Right can also be & -, which means that the Left descriptor is turned off, for example, 2

For

< 时表示以读模式复制 Right 到 Left, 此时如果没有指定 Left 的话, 则为默认值 0; 当为 >

Indicates that Right is copied to Left in write mode. If no Left is specified, the default value is 1

There can be no spaces between Operation and Left

When Right is a file name, Operation and Right can have spaces, otherwise there can be no spaces

When there is more than one file descriptor, it is performed from left to right. For example, stdin and stdout can be exchanged by using the command cmd 3 > & 1 1 > & 2 2 > & 3 3 > & &.

Let's use the following example to verify that the above file descriptor exchange works:

First redirect the default stderr to the file / tmp/stderr so that the error output will not be seen in the terminal; after swapping stderr and stdout, we can see the normal output of the command in the / tmp/stderr file

Let's start the experiment:

➜test exec 2 > / tmp/stderr➜ test lsa.txt ➜test ls 3 > & 11 > & 22 > & 33 > & -➜ test cat / tmp/stderra.txt

It's consistent with our expectations!

Some examples

Reload stdin with a file:

➜test cat 0

< a.txthello➜ test cat < a.txt # same with last commandhello 把 stderr 和 stdout 都过滤掉 ls not_exist 1>

/ dev/zero 2 > & another wayls not_exist & > / dev/zero

Handle the error output of the previous command:

➜blog git: (hexo) ls not_exist 2 > & 1 | sed 's/not_exist/error/g'ls: error: No such file or directory# another way➜ blog git: (hexo) ls not_exist | & sed' s/not_exist/error/g'ls: error: No such file or directory

Transfer standard output to error output: echo hello 1 > & 2

Process Substitution

Two special operations are provided in bash, both of which can be used directly as file names:

(cmd): can be thought of as a writable file, and cmd will accept input and process it.

Example

Ls: keep_error: No such file or directory ➜blog git: (hexo)

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

References

File Descriptors in Bourne shellProcess Substitution

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

Servers

Wechat

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

12
Report