簡體   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