【问题标题】:What is the purpose of 2>nul in this Stack Overflow answer?这个 Stack Overflow 答案中 2>nul 的目的是什么?
【发布时间】:2016-08-20 02:52:57
【问题描述】:

This stackoverflow answer 包含以下代码:

dir /a-d "C:\PLUS\ADMIN\BATCH\*" >nul 2>nul && (
    start "" cmd /c "@echo Files in the directory! &@echo(&@pause
) || ( 
    echo there are no files
)

>nul 是将stdout 重定向到nul2>nul 的用途是什么,在什么情况下需要它?

【问题讨论】:

  • 这不是被问到的,所以我在这里发表评论。 Start "" 打开第二个控制台窗口,运行 cmd /c 启动一个新的命令interperter。在 ...@pause 之后可能有一个结束引号 ",但它不是必需的。如果运行 Windows,请打开控制台窗口并输入 help start 以获取有关启动的更多信息,并帮助 cmd 获取有关 cmd 的信息。
  • @RockPaperLizard 请参阅 Microsoft 关于 Using command redirection operators 的文章。
  • @Mofi 非常感谢。很棒的文章。顺便说一句,那篇文章列在 Windows XP 的命令行参考 下。 MS 是否有类似的 Windows 7 命令行参考?
  • @RockPaperLizard 是的,Windows Server 2003、Windows Vista、Windows XP、Windows Server 2008、Windows 7、Windows Server 2003 R2、Windows Server 2008 R2、Windows Server 2000 有一个更新的Command-Line Reference , Windows Server 2012, Windows 8。但它不包含有关命令重定向运算符的文章。
  • @RockPaperLizard 您可能还对来自 SS64 An A-Z Index of the Windows CMD command line 的 SS64 文章 Command redirections, Pipes 感兴趣。由于考虑了所有 Windows 版本,所以关于 SS64 的文档通常更好。

标签: windows batch-file command-line output stdout


【解决方案1】:

对于 Windows 控制台程序,2> 是标准错误。如果正在运行的程序可能会向标准错误输出文本,则需要它。

【讨论】:

  • @RockPaperLizard stderr 不同的流。在 cmd 提示符下尝试dir nosuchfile.whatever >nul,您仍然会看到stderr 输出“找不到文件”。
  • @dxiv 很好的例子。谢谢。
  • @RockPaperLizard:在命令提示符下试试这个:dir /B *.txt 2>Error.msg & findstr /N /A:4E "^" *.msg,然后用不存在的扩展名更改*.txt... ;-)
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 2010-09-07
  • 2017-07-07
  • 2021-05-23
  • 2020-12-01
  • 2013-05-26
  • 1970-01-01
  • 2015-11-13
相关资源
最近更新 更多