>/dev/null吃掉输出流吗? [英] How does > /dev/null eat up output streams?
问题描述
我在bash编程中经常使用/dev/null
将不必要的输出发送到黑洞中.
I've used /dev/null
a lot in bash programming to send unnecessary output into a black hole.
例如,以下命令:
$ echo 'foo bar' > /dev/null
$
不会 echo
任何东西.我读过/dev/null
是一个空文件,用于通过重定向处理不需要的输出.但是,这种处置究竟是如何进行的呢?我无法想象/dev/null
将内容写入文件,然后立即删除该文件.那么,当您重定向到此文件时实际发生了什么?
Will not echo
anything. I've read that /dev/null
is an empty file used to dispose of unwanted output through redirection. But how exactly does this disposal take place? I can't imagine /dev/null
writing the content to a file and then immediately deleting that file. So what actually happens when you redirect to this file?
推荐答案
>/dev/null
将命令标准输出重定向到null设备,这是一个特殊的设备,它将丢弃写入的信息做到这一点.所有这些都是通过file_operations实现的( drivers/char/mem.c
如果您想看一下自己的话):
>/dev/null
redirects the command standard output to the null device, which is a special device which discards the information written to it.
It's all implemented via file_operations (drivers/char/mem.c
if you're curious to look yourself):
static const struct file_operations null_fops = {
.llseek = null_lseek,
.read = read_null,
.write = write_null,
.splice_write = splice_write_null,
};
write_null是您写入/dev/null时所调用的名称.它总是返回与您写入的字节数相同的字节数:
write_null is what's called when you write to /dev/null. It always returns the same number of bytes that you write to it:
static ssize_t write_null(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
return count;
}
就是这样.缓冲区将被忽略.
That's it. The buffer is just ignored.
这篇关于>/dev/null吃掉输出流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!