本文共 3814 字,大约阅读时间需要 12 分钟。
/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading.
Linux中的/ dev / null是空设备文件。 这将丢弃写入其中的所有内容,并在读取时返回EOF 。
This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.
这是一个命令行程序,起到了吸尘的作用,它吸走了扔给它的所有东西。
Let’s take a look at understanding what it means, and what we can do with this file.
让我们看一下它的含义,以及我们可以使用此文件做什么。
This will return an End of File (EOF) character if you try to read it using the .
如果您尝试使用读取文件结束符( EOF ),它将返回文件结束符( EOF )。
cat /dev/null
This is a valid file, which can be verified using
这是有效的文件,可以使用进行验证
stat /dev/null
This gives me an output of
这给了我一个输出
File: /dev/null Size: 0 Blocks: 0 IO Block: 4096 character special fileDevice: 6h/6d Inode: 5 Links: 1 Device type: 1,3Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2020-02-04 13:00:43.112464814 +0530Modify: 2020-02-04 13:00:43.112464814 +0530Change: 2020-02-04 13:00:43.112464814 +0530
This shows that this file has a size of 0 bytes, has zero blocks allocated to it. The file permissions are also set that anyone can read/write to it, but cannot execute it.
这表明该文件的大小为0字节,分配了零个块。 还设置了文件权限,任何人都可以对其进行读写,但不能执行它。
Since it is not an executable file, we cannot use piping using |
operator to redirect to /dev/null
. The only way is to use file redirections (>
, >>
, or <
, <<
).
由于它不是可执行文件,因此不能使用|
运算符以重定向到/dev/null
。 唯一的方法是使用文件重定向( >
, >>
或<
, <<
)。
The below diagram shows that /dev/null
is indeed a valid file.
下图显示/dev/null
确实是有效文件。
Let’s now take a look at some common use cases for /dev/null
.
现在让我们看一下/dev/null
一些常见用例。
We can discard any output of a script that we use by redirecting to /dev/null
.
我们可以通过重定向到/dev/null
来丢弃脚本使用的任何输出。
For example, we can try discarding echo
messages using this trick.
例如,我们可以尝试使用此技巧丢弃echo
消息。
echo 'Hello from JournalDev' > /dev/null
You will not get any output since it is discarded!
您将不会获得任何输出,因为它将被丢弃!
Let’s try running a command incorrectly and pipe it’s output to /dev/null
.
让我们尝试错误地运行命令,并将其输出通过管道传递到/dev/null
。
cat --INCORRECT_OPTION > /dev/null
We still get an output like this:
我们仍然得到这样的输出:
cat: unrecognized option '--INCORRECT'Try 'cat --help' for more information.
Why is this happening? This is because the error messages are coming from stderr
, but we are only discarding output from stdout
.
为什么会这样呢? 这是因为错误消息来自stderr
,但是我们仅丢弃stdout
。
We need to take stderr
into account as well.
我们还需要考虑stderr
。
Let us redirect the stderr to /dev/null, along with stdout. We can use the file descriptor for stderr(=2) for this.
让我们将stderr和stdout一起重定向到/ dev / null 。 为此,我们可以使用stderr( = 2 )的文件描述符。
cat --INCORRECT_OPTION > /dev/null 2>/dev/null
This will give us what we need!
这将给我们我们所需要的!
There is another way of doing the same; by redirecting stderr to stdout first, and then redirect stdout to /dev/null.
还有另一种方法可以做到这一点。 首先将stderr重定向到stdout,然后将stdout重定向到/ dev / null 。
The syntax for this will be:
语法如下:
command > /dev/null 2>&1
Notice the 2>&1
at the end. We redirect stderr(2) to stdout(1). We use &1
to mention to the shell that the destination file is a file descriptor and not a file name.
请注意最后的2>&1
。 我们将stderr( 2 )重定向到stdout( 1 )。 我们用&1
向shell提及目标文件是文件描述符而不是文件名。
cat --INCORRECT_OPTION > dev/null 2>&1
So if we use 2>1
, we will only redirect stderr
to a file called 1
. This is not what we want!
因此,如果我们使用2>1
,我们将仅将stderr
重定向到名为1
的文件。 这不是我们想要的!
Hopefully, this clears things up a bit, so that you can now use /dev/null in Linux, knowing what it means! Feel free to ask questions in the comment section below.
希望这可以解决问题,以便您现在就可以在Linux中使用/ dev / null了,这意味着什么! 随时在下面的评论部分中提问。
翻译自:
转载地址:http://gwlzd.baihongyu.com/