Windows 上有 /dev/null 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/313111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:46:30  来源:igfitidea点击:

Is there a /dev/null on Windows?

windows

提问by Ove S

What is the equivalent of /dev/nullon Windows?

/dev/null在 Windows 上相当于什么?

回答by Jon Skeet

I think you want NUL, at least within a command prompt or batch files.

我认为你想要NUL,至少在命令提示符或批处理文件中。

For example:

例如:

type c:\autoexec.bat > NUL

doesn't create a file.

不创建文件。

(I believethe same is true if you try to create a file programmatically, but I haven't tried it.)

(我相信如果您尝试以编程方式创建文件也是如此,但我还没有尝试过。)

In PowerShell, you want $null:

在 PowerShell 中,您需要$null

echo 1 > $null

回答by strager

According to this message on the GCC mailing list, you can use the file "nul" instead of /dev/null:

根据GCC 邮件列表上的这条消息,您可以使用文件“nul”而不是 /dev/null:

#include <stdio.h>

int main ()
{
    FILE* outfile = fopen ("/dev/null", "w");
    if (outfile == NULL)
    {
        fputs ("could not open '/dev/null'", stderr);
    }
    outfile = fopen ("nul", "w");
    if (outfile == NULL)
    {
        fputs ("could not open 'nul'", stderr);
    }

    return 0;
}

(Credits to Dannyfor this code; copy-pasted from his message.)

(此代码归功于Danny;从他的消息中复制粘贴。)

You can also use this special "nul" file through redirection.

您还可以通过重定向使用这个特殊的“nul”文件。

回答by Jerry

NULin Windows seems to be actually a virtual path in any folder. Just like .., .in any filesystem.

NUL在 Windows 中似乎实际上是任何文件夹中的虚拟路径。就像..,.在任何文件系统中一样。

Use any folder followed with NUL will work.

使用任何带有 NUL 的文件夹都可以。

Example,

例子,

echo 1 > nul
echo 1 > c:\nul
echo 1 > c:\users\nul
echo 1 > c:\windows\nul

have the same effect as /dev/nullon Linux.

/dev/null在 Linux 上的效果相同。

This was tested on Windows 7, 64 bit.

这是在 64 位 Windows 7 上测试的。

回答by Foredecker

Jon Skeet is correct. Here is the Nul Device Driverpage in the Windows Embedded documentation (I have no idea why it's not somewhere else...).

乔恩·斯基特是对的。这是Windows Embedded 文档中的Nul Device Driver页面(我不知道为什么它不在其他地方......)。

Here is another:

这是另一个:

回答by ava

NULworks programmatically as well. E.g. the following:

NUL也以编程方式工作。例如以下内容:

freopen("NUL", "w", stderr);

works as expected without creating a file. (MSVC++ 12.0)

在不创建文件的情况下按预期工作。(MSVC++ 12.0)

回答by Marco

If you need to perform in Microsoft Windows the equivalent of a symlink to /dev/nullin Linux you would open and administrator's cmdand type:

如果您需要在 Microsoft Windows 中执行相当于/dev/nullLinux中的符号链接,您可以打开管理员cmd并键入:

For files:

对于文件:

mklink c:\path\to\file.ext NUL:

Or, for directories:

或者,对于目录:

mklink /D c:\path\to\dir NUL:

This will keep the file/direcotry always at 0 byte, and still return success to every write attempt.

这将使文件/目录始终保持在 0 字节,并且每次写入尝试仍然返回成功。

回答by HaSeeB MiR

You have to use startand $NULfor this in Windows PowerShell:

您必须在Windows PowerShell 中为此使用start$NUL

Type in this command assuming mySumis the name of your application and 510are command line arguments you are sending.

假设mySum是您的应用程序的名称并且510是您要发送的命令行参数,请输入此命令。

start .\mySum  5 10 > $NUL 2>&1

The startcommand will start a detached process, a similar effect to &. The /Boption prevents start from opening a new terminal window if the program you are running is a console application. and NULis Windows' equivalent of /dev/null. The 2>&1at the end will redirect stderr to stdout, which will all go to NUL.

start命令将启动一个分离的进程,与&. /B如果您正在运行的程序是控制台应用程序,则该选项会阻止 start 打开新的终端窗口。并且NUL是 Windows 的等价物/dev/null。将2>&1在年底将标准错误重定向到标准输出,这将都去NUL