简体   繁体   English

重定向到文件时,Write-Host 会导致空行

[英]Write-Host causes blank lines when redirected to a file

The snippet片段

powershell { Write-Host "a"; Write-Host "b" } > test.txt; Write-Host "File contents:"; cat test.txt; rm test.txt

prints印刷

a
b
File contents:
a


b


Why are there 2 blank lines after every Write-Host call in the text file?为什么文本文件中的每个Write-Host调用后都有 2 个空白行?

Even more confusing is the behavior when we redirect all streams to a file:更令人困惑的是当我们将所有流重定向到文件时的行为:

powershell { Write-Host "a"; Write-Host "b" } *> test.txt; Write-Host "File contents:"; cat test.txt; rm test.txt

prints印刷

File contents:
a


b


a
b

Now the file contains everything twice, first with 2 blank lines and then normally.现在该文件包含所有内容两次,首先是 2 个空行,然后是正常的。 Why does the file contain everything twice now?为什么文件现在包含所有内容两次?

Your script does the following:您的脚本执行以下操作:

Write-Host "a" Writes a[CR][LF] to stdout . Write-Host "a"a[CR][LF]写入stdout Then powershell for some reason* adds [LF] to it and stores it as an element in return list.然后 powershell 出于某种原因* 添加[LF]并将其作为元素存储在返回列表中。 When it prints return list, it prints each element as [$value.toString()][CR][LF] .当它打印返回列表时,它将每个元素打印为[$value.toString()][CR][LF]

This gives you result that Write-Host "a" becomes a[CR][LF][LF][CR][LF] in output.这会给您结果, Write-Host "a"在 output 中变为a[CR][LF][LF][CR][LF]

* Needs investigation. *需要调查。 Possibly it's if value from stdout and ends with [CR][LF] .可能是if value from stdout and ends with [CR][LF] Again, stdout is not supposed to work as pipeline in powershell, because powershell has it's own pipeline of objects.同样,stdout 不应该在 powershell 中用作管道,因为 powershell 有自己的对象管道。


Powershell's Write-Host is not designed to be catched to output. Powershell 的Write-Host并非旨在被 output 捕获。 Instead, there is Write-Output command, or return .相反,有Write-Output命令或return

There is no good built-in command to output data both to file and to stdout except Tee-Object , but this does some different thing.除了Tee-Object之外,对于文件和标准输出的 output 数据没有好的内置命令,但这有一些不同的作用。

You should avoid using powershell's stdout output as stdin for other commands, as it can cantain better-look enchancements , truncated parts or line wraps that you can not expect.您应该避免使用 powershell 的stdout stdin作为其他命令的标准输入,因为它可以包含better-look enchancementstruncated parts或您无法预料的line wraps If you can not avoid it, use Write-Output and void any possible output with [void] or .. | Out-Null如果无法避免,请使用Write-Output并使用[void].. | Out-Null取消任何可能的 output .. | Out-Null . .. | Out-Null All unhandled returns from any operations (like, new-item ) will be passed to stdout来自任何操作(如new-item )的所有未处理的返回都将传递给stdout


powershell {Write-Output "a" ;Write-Output "b"} > test.txt; Write-Host "File contents:"; cat test.txt; 
powershell {Write-Output "a" ;Write-Output "b"} | Out-File 'test.txt' ; Write-Host "File contents:"; cat test.txt; 
powershell { @('A';'B') | Out-File 'test.txt' } ; Write-Host "File contents:"; cat test.txt; 
powershell { @('A';'B') | Tee-Object -FilePath 'test.txt' | % { Write-Host "+ $_" } } | Out-Null ; Write-Host "File contents:"; cat test.txt; 
powershell {Write-Host "a" -NoNewline;Write-Host "b" -NoNewline} > test.txt; Write-Host "File contents:"; cat test.txt; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM