繁体   English   中英

在 PowerShell 中抑制 Git 命令的输出

[英]Suppress output from Git command in PowerShell

作为脚本的一部分,我正在运行git clean -fd 它通常会输出一堆已清理的文件。 我想抑制这个输出。

我试过git clean -fd | Out-Null git clean -fd | Out-Null但它似乎不起作用。 谷歌搜索没有返回git抑制输出的选项,那么我可以使用另一种 PowerShell 方法吗? 还是我做错了什么? 值得注意的是,PowerShell 脚本本身是从.bat文件中执行的。

只需添加选项-q

git clean -fdq

博士的有用答案为手头的案例提供了最佳解决方案。

至于为什么... | Out-Null ... | Out-Null无效:

Out-Null仅抑制来自外部程序(例如git stdout输出,但不抑制stderr输出。

git与许多 CLI(控制台/终端程序)一样,使用 stderr 流不仅报告错误,还报告状态信息- 基本上,任何不是 data 的东西。

为了抑制stdout和stderr输出,使用*> $null

git clean -fd *> $null

注意: *> $null抑制所有输出流 虽然外部程序只有2 个(stdout 和 stderr),但将*>$nullPowerShell 本地命令会使所有 6 个输出流静音。

有关更多信息,请参阅about_Redirection


可选阅读:来自外部程序的选择性流重定向:

基于nmbell 的反馈:

  • >$null (或1>$null )可用于选择性地抑制stdout输出,这实际上与| Out-Null相同| Out-Null | Out-Null

  • 2>$null可用于选择性地抑制stderr输出。

  • *>$null ,如上所述,使两个(所有)流静音。

当然,除了$null用于抑制输出之外,重定向目标也可以是文件(名称或路径)。

笔记:

  • PowerShell 在其管道中逐行处理来自外部程序的输出,如果输出在变量( $out = ... ) 中捕获并包含2 行或更多行,则将其存储为数组( [object[]] )行(字符串)。

  • PowerShell 只在发送和接收数据时与外部程序“对话”(使用字符串),这意味着字符编码问题可能会发挥作用。

  • 有关两个方面的更多信息,请参阅此答案


场景示例

设置:

# Construct a platform-appropriate command, stored in a script block ({ ... }) 
# that calls an external program (the platform-native shell) that outputs
# 1 line of stdout and 1 line of stderr output each, and can later be 
# invoked with `&`, the call operator.
$externalCmd = if ($env:OS -eq 'Windows_NT') {     # Windows
                 { cmd /c 'echo out & echo err >&2' } 
               } else {                            # Unix (macOS, Linux)
                 { sh -c 'echo out; echo err >&2' } 
               }

捕获标准输出,标准错误传递通过

PS> $captured = & $externalCmd; "Captured: $captured"
err            # Stderr output was *passed through*
Captured: out  # Captured stdout output.

捕获 stdout,抑制stderr 输出,使用2>$null

PS> $captured = & $externalCmd 2>$null; "Captured: $captured"
Captured: out  # Captured stdout output - stderr output was suppressed.

捕获标准输出和标准错误,与*>&1

PS> $captured = & $externalCmd *>&1 | % ToString; "Captured: $captured"
Captured: out err  # *Combined* stdout and stderr output.

笔记:

  • % ToStringForEach-Object ToString缩写,它在每个输出对象上调用.ToString()方法,以确保 PowerShell 包装stderr行的System.Management.Automation.ErrorRecord实例被转换回strings
  • $captured接收行的 2 元素数组( [object[]] ) - 分别包含 stdout 和 stderr 行作为元素; 在这种情况下, PowerShell 的字符串插值将它们转换为单行、空格分隔的字符串。

捕获stderr ,抑制 stdout:

PS> $captured = 
      & $externalCmd *>&1 | 
        ? { $_ -is [System.Management.Automation.ErrorRecord] } | 
          % ToString; "Captured: $captured"
Captured: err  # Captured stderr output *only*.

笔记:

  • ? { $_ -is [System.Management.Automation.ErrorRecord] } ? { $_ -is [System.Management.Automation.ErrorRecord] }是缩写
    Where-Object { $_ -is [System.Management.Automation.ErrorRecord] } ,它仅传递 stderr 行 - 可通过正在测试的包装器类型识别 - 通过,并且% ToString再次将它们转换回字符串。

  • 这种技术既不明显也不方便; GitHub 建议 #4332提出了一种语法,例如2> variable:stderr以支持将流重定向到variables ,例如$stderr在这种情况下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM