bash - PowerShell - 如何从$(bash)管道返回

可以从管道返回:


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



我想显示最后一行和进度,例如


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



怎么办?

时间: 原作者:


#!/bin/bash



# redirect the file descriptor 3 to 1 (stdout)


exec 3>&1



longRunningCmd="flutter build ios --release --no-codesign"



# use tee to copy the command's output to file descriptor 3 (stdout) while 


# capturing 1 (stdout) into a variable


output=$(eval"$longRunningCmd" | tee >(cat ->&3) )



# last line of output


lastline=$(printf"%s""$output" | tail -n 1)



echo"$lastline"



原作者:

PowerShell解决方案(假设你问题中的代码是PowerShell ):

我不确定flutter如何报告进度,但以下方法有效:

如果所有内容都指向stdout:


$OUTPUT = flutter build ios --release --no-codesign | % {


 Write-Host $_ # print to host (console)


 $_ # send through pipeline


} | select -last 1



如果进度消息转到stderr:


$OUTPUT = flutter build ios --release --no-codesign 2>&1 | % {


 Write-Host $_.ToString() # print to host (console)


 if ($_ -is [string]) { $_ } # send only stdout through pipeline


} | select -last 1




原作者:

在管道中使用写入进度。为了保持可读管道,我编写了一个函数

function Write-PipedProgress{ < #


.SYNOPSIS


 Insert this function in a pipeline to display progress bar to user



.EXAMPLE


 $Result = (Get-250Items | 


 Write-PipedProgress -PropertyName Name -Activity"Audit services" -ExpectedCount 250 |


 Process-ItemFurther)



>

[cmdletBinding()]


param(


 [parameter(Mandatory=$true,ValueFromPipeline=$true)]


 $Data,


 [string]$PropertyName=$null,


 [string]$Activity,


 [int]$ExpectedCount=100


 )



begin {


 Write-Verbose"Starting $($MyInvocation.MyCommand)"


 $ItemCounter = 0


}


process {


 Write-Verbose"Start processing of $($MyInvocation.MyCommand)($Data)"



 try {


 $ItemCounter++


 # (3) mitigate unexpected additional input volume"


 if ($ItemCounter -lt $ExpectedCount) {


 $StatusProperty = if ($propertyName) { $Data.$PropertyName } > > else {""}


 $StatusMessage ="Processing $ItemCounter th $StatusProperty"


 $statusPercent = 100 * $ItemCounter / $ExpectedCount


 Write-Progress -Activity $Activity -Status $StatusMessage -> > PercentComplete $statusPercent


 } else {


 Write-Progress -Activity $Activity -Status"taking longer than expected" -PercentComplete 99


 }



 # return input data to next element in pipe


 $Data



 } catch {


 throw


 }


 finally {


 Write-Verbose"Complete processing of $Data in > $($MyInvocation.MyCommand)"


 }



}


end {


 Write-Progress -Activity $Activity -Completed


 Write-Verbose"Complete $($MyInvocation.MyCommand) - processed $ItemCounter items"


}



}

希望这有帮助; -)

原作者:

我相信这可以工作。


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



原作者:
...