[英]Suppress output from Git command in PowerShell
As part of a script I am running git clean -fd
.作为脚本的一部分,我正在运行
git clean -fd
。 It typically outputs a bunch of files that are cleaned up.它通常会输出一堆已清理的文件。 I would like to suppress this output.
我想抑制这个输出。
I tried git clean -fd | Out-Null
我试过
git clean -fd | Out-Null
git clean -fd | Out-Null
but it did not seem to work. git clean -fd | Out-Null
但它似乎不起作用。 Googling returned no options for git
to suppress output, so is there another PowerShell method I can use?谷歌搜索没有返回
git
抑制输出的选项,那么我可以使用另一种 PowerShell 方法吗? Or am I doing something wrong?还是我做错了什么? It may be worth noting that the PowerShell script itself is being executed from within a
.bat
file.值得注意的是,PowerShell 脚本本身是从
.bat
文件中执行的。
只需添加选项-q
:
git clean -fdq
phd's helpful answer offers the best solution for the case at hand.博士的有用答案为手头的案例提供了最佳解决方案。
As for why ... | Out-Null
至于为什么
... | Out-Null
... | Out-Null
wasn't effective: ... | Out-Null
无效:
Out-Null
suppresses only stdout output from external programs such as git
, but not stderr output. Out-Null
仅抑制来自外部程序(例如git
stdout输出,但不抑制stderr输出。
git
, like many CLIs (console / terminal programs), uses the stderr stream not just to report errors , but also for status information - basically, anything that's not data . git
与许多 CLI(控制台/终端程序)一样,使用 stderr 流不仅报告错误,还报告状态信息- 基本上,任何不是 data 的东西。
To suppress both stdout and stderr output, use *> $null
:为了抑制stdout和stderr输出,使用
*> $null
:
git clean -fd *> $null
Note: *> $null
suppresses all output streams ;注意:
*> $null
抑制所有输出流; while external programs only have 2 (stdout and stderr), applying *>$null
to a PowerShell-native command silences all 6 output streams.虽然外部程序只有2 个(stdout 和 stderr),但将
*>$null
到PowerShell 本地命令会使所有 6 个输出流静音。
See about_Redirection for more information.有关更多信息,请参阅about_Redirection 。
Building on feedback from nmbell :基于nmbell 的反馈:
>$null
(or 1>$null
) can be used to suppress stdout output selectively , which is effectively the same as | Out-Null
>$null
(或1>$null
)可用于选择性地抑制stdout输出,这实际上与| Out-Null
相同| Out-Null
| Out-Null
. | Out-Null
。
2>$null
can be used to suppress stderr output selectively . 2>$null
可用于选择性地抑制stderr输出。
*>$null
, as discussed above, silences both (all) streams. *>$null
,如上所述,使两个(所有)流静音。
Of course, instead of $null
for suppressing output, the redirection target may also may be a file (name or path).当然,除了
$null
用于抑制输出之外,重定向目标也可以是文件(名称或路径)。
Note:笔记:
PowerShell processes output from external programs line by line in its pipeline, and if the output is captured in a variable ( $out = ...
) and comprises 2 or more lines , it is stored as an array ( [object[]]
) of lines (strings). PowerShell 在其管道中逐行处理来自外部程序的输出,如果输出在变量(
$out = ...
) 中捕获并包含2 行或更多行,则将其存储为数组( [object[]]
)行(字符串)。
PowerShell only ever "speaks text" (uses strings) with external programs, both when sending and receiving data, which means that character-encoding issues may come into play. PowerShell 只在发送和接收数据时与外部程序“对话”(使用字符串),这意味着字符编码问题可能会发挥作用。
See this answer for more information on both these aspects.有关这两个方面的更多信息,请参阅此答案。
Scenarios with examples :场景示例:
Setup:设置:
# 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' }
}
Capture stdout, pass stderr through :捕获标准输出,标准错误传递通过:
PS> $captured = & $externalCmd; "Captured: $captured"
err # Stderr output was *passed through*
Captured: out # Captured stdout output.
Capture stdout, suppress stderr output, with 2>$null
:捕获 stdout,抑制stderr 输出,使用
2>$null
:
PS> $captured = & $externalCmd 2>$null; "Captured: $captured"
Captured: out # Captured stdout output - stderr output was suppressed.
Capture both stdout and stderr, with *>&1
:捕获标准输出和标准错误,与
*>&1
:
PS> $captured = & $externalCmd *>&1 | % ToString; "Captured: $captured"
Captured: out err # *Combined* stdout and stderr output.
Note:笔记:
% ToString
is short for ForEach-Object ToString
, which calls the .ToString()
method on each output object, which ensures that the System.Management.Automation.ErrorRecord
instances that PowerShell wraps stderr lines in are converted back to strings . % ToString
是ForEach-Object ToString
缩写,它在每个输出对象上调用.ToString()
方法,以确保 PowerShell 包装stderr行的System.Management.Automation.ErrorRecord
实例被转换回strings 。$captured
receives a 2-element array ( [object[]]
) of lines - containing the stdout and stderr line as elements, respectively; $captured
接收行的 2 元素数组( [object[]]
) - 分别包含 stdout 和 stderr 行作为元素; it is PowerShell's string interpolation that turns them into a single-line, space-separated string in this case. Capture only stderr , suppress stdout:仅捕获stderr ,抑制 stdout:
PS> $captured =
& $externalCmd *>&1 |
? { $_ -is [System.Management.Automation.ErrorRecord] } |
% ToString; "Captured: $captured"
Captured: err # Captured stderr output *only*.
Note:笔记:
? { $_ -is [System.Management.Automation.ErrorRecord] }
? { $_ -is [System.Management.Automation.ErrorRecord] }
is short for ? { $_ -is [System.Management.Automation.ErrorRecord] }
是缩写Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }
, which passes only stderr lines - identifiable via the wrapper type being tested for - through, and % ToString
again converts them back to strings. Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }
,它仅传递 stderr 行 - 可通过正在测试的包装器类型识别 - 通过,并且% ToString
再次将它们转换回字符串。
This technique is neither obvious nor convenient;这种技术既不明显也不方便; GitHub suggestion #4332 proposes a syntax such as
2> variable:stderr
to support redirecting streams to variables , such as $stderr
in this case. GitHub 建议 #4332提出了一种语法,例如
2> variable:stderr
以支持将流重定向到variables ,例如$stderr
在这种情况下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.