powershell 如何同时捕获外部命令输出并打印到终端

hmmo2u0o  于 2023-10-18  发布在  Shell
关注(0)|答案(4)|浏览(155)

我可以从以下位置返回:

  1. $OUTPUT = $(flutter build ios --release --no-codesign | tail -1)

我想从构建中获得最后一行并显示进度,类似于

  1. $OUTPUT = $(flutter build ios --release --no-codesign | out | tail -1)

其中假设的out实用程序也将输出发送到终端。
你知道怎么做吗?

aemubtdh

aemubtdh1#

注意事项:

  • 在类Unix的平台上,使用外部程序输出js2010's elegant tee /dev/tty solution是最简单的。
  • 下面的解决方案也适用于Windows**,可能会对PowerShell中的逐行处理 * 外部程序 * 输出感兴趣。
  • 一个 * 通用 * 解决方案**,也适用于PowerShell原生命令可以输出的 * 复杂对象 *,需要不同的方法
  • PowerShell(Core)7+ 中,请使用以下命令:
  1. # PS v7+ only. Works on both Windows and Unix
  2. ... | Tee-Object ($IsWindows ? '\\.\CON' : '/dev/tty')
  • GitHub issue #19827是一个绿灯,但尚未(截至PowerShell(Core)7.3.x)实现的改进,将允许简化

($IsWindows ? '\\.\CON': '/dev/tty')-Host

  • Windows PowerShell 中,Tee-Object不幸地不支持以CON为目标,需要一个利用Out-Host的 * 代理函数 *-请参阅this answer

一个 PowerShell 解决方案(假设你的问题中的代码是PowerShell[1]):
我不确定flutter如何报告其进度,但以下方法可能有效:
如果一切都转到 stdout

  1. $OUTPUT = flutter build ios --release --no-codesign | % {
  2. Write-Host $_ # print to host (console)
  3. $_ # send through pipeline
  4. } | select -Last 1

注意:%ForEach-Object的内置别名,selectSelect-Object的内置别名。
如果进度消息转到 stderr

  1. $OUTPUT = flutter build ios --release --no-codesign 2>&1 | % {
  2. Write-Host $_.ToString() # print to host (console)
  3. if ($_ -is [string]) { $_ } # send only stdout through pipeline
  4. } | select -Last 1

[1]正如赋值 * 的LHS中变量名 * 中的$符号和=周围的空格所证明的那样
$OUTPUT = ),这两个都不能在类似bash/POSIX的shell中正常工作。

展开查看全部
63lcw9qa

63lcw9qa2#

我假设你是说bash,因为据我所知,powershell中没有tail
下面是如何在将命令捕获到变量中的同时查看命令的输出。

  1. #!/bin/bash
  2. # redirect the file descriptor 3 to 1 (stdout)
  3. exec 3>&1
  4. longRunningCmd="flutter build ios --release --no-codesign"
  5. # use tee to copy the command's output to file descriptor 3 (stdout) while
  6. # capturing 1 (stdout) into a variable
  7. output=$(eval "$longRunningCmd" | tee >(cat - >&3) )
  8. # last line of output
  9. lastline=$(printf "%s" "$output" | tail -n 1)
  10. echo "$lastline"
展开查看全部
edqdpe6u

edqdpe6u3#

我在管道中使用写进度。为了保持管道的可读性,我写了一个函数
函数Write-PipedProgress{ <#

  1. .SYNOPSIS
  2. Insert this function in a pipeline to display progress bar to user
  3. .EXAMPLE
  4. $Result = (Get-250Items |
  5. Write-PipedProgress -PropertyName Name -Activity "Audit services" -ExpectedCount 250 |
  6. Process-ItemFurther)

联系我们

  1. [cmdletBinding()]
  2. param(
  3. [parameter(Mandatory=$true,ValueFromPipeline=$true)]
  4. $Data,
  5. [string]$PropertyName=$null,
  6. [string]$Activity,
  7. [int]$ExpectedCount=100
  8. )
  9. begin {
  10. Write-Verbose "Starting $($MyInvocation.MyCommand)"
  11. $ItemCounter = 0
  12. }
  13. process {
  14. Write-Verbose "Start processing of $($MyInvocation.MyCommand)($Data)"
  15. try {
  16. $ItemCounter++
  17. # (3) mitigate unexpected additional input volume"
  18. if ($ItemCounter -lt $ExpectedCount) {
  19. $StatusProperty = if ($propertyName) { $Data.$PropertyName } > > else { ""}
  20. $StatusMessage = "Processing $ItemCounter th $StatusProperty"
  21. $statusPercent = 100 * $ItemCounter / $ExpectedCount
  22. Write-Progress -Activity $Activity -Status $StatusMessage -> > PercentComplete $statusPercent
  23. } else {
  24. Write-Progress -Activity $Activity -Status "taking longer than expected" -PercentComplete 99
  25. }
  26. # return input data to next element in pipe
  27. $Data
  28. } catch {
  29. throw
  30. }
  31. finally {
  32. Write-Verbose "Complete processing of $Data in > $($MyInvocation.MyCommand)"
  33. }
  34. }
  35. end {
  36. Write-Progress -Activity $Activity -Completed
  37. Write-Verbose "Complete $($MyInvocation.MyCommand) - processed $ItemCounter items"
  38. }

}
希望这有帮助;- )

展开查看全部
kqqjbcuj

kqqjbcuj4#

我相信这会起作用,至少在OSX或Linux PowerShell(甚至是Linux的Windows子系统)中有这些命令可用。我用“ls”而不是“flutter”来测试它。真的有“out”命令吗?

  1. $OUTPUT = bash -c 'flutter build ios --release --no-codesign | tee /dev/tty | tail -1'

或者,假设tee没有别名为tee对象。事实上,tee-object也可以工作。

  1. $OUTPUT = flutter build ios --release --no-codesign | tee /dev/tty | tail -1

它也可以使用$(),但你不需要它。在powershell中,它用于合并多个管道。

相关问题