博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux中的/ dev / null
阅读量:2529 次
发布时间:2019-05-11

本文共 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.

让我们看一下它的含义,以及我们可以使用此文件做什么。



/ dev / null属性 (/dev/null Properties)

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确实是有效文件。

File Tables
File Tables
文件表

Let’s now take a look at some common use cases for /dev/null.

现在让我们看一下/dev/null一些常见用例。



在Linux中重定向到/ dev / null (Redirection to /dev/null in Linux)

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

丢弃错误消息 (Discard error messages)

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的文件。 这不是我们想要的!



结论 (Conclusion)

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了,这意味着什么! 随时在下面的评论部分中提问。



参考资料 (References)



翻译自:

转载地址:http://gwlzd.baihongyu.com/

你可能感兴趣的文章
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
在线教育工具—白板系统的迭代1——bug监控排查
查看>>
121. Best Time to Buy and Sell Stock
查看>>
hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
查看>>
安装php扩展
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>