powershellterminaltee

How to simultaneously capture external command output and print it to the terminal


Can I pipe back from:

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

I would like to get both the last line from the build AND show progress, something like

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

where the hypothetical out utility would also send the output to the terminal.

Do you know how?


Solution

  • Note:


    A PowerShell solution (given that the code in your question is PowerShell[1]):

    I'm not sure how flutter reports its progress, but the following may work:

    If everything goes to stdout:

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

    Note: % is the built-in alias for ForEach-Object, and select the one for Select-Object.

    If progress messages go to 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
    

    [1] As evidenced by the $ sigil in the variable name in the LHS of an assignment and the spaces around =
    ($OUTPUT = ), neither of which would work as intended in bash / POSIX-like shells.