0

when run two commands which connected with a pipeline

PS> cmdA | cmbB

e.g.
1) in powershell
PS> ls -Recurse | more

2) in CMD.exe
C:\> dir /s | more

you'll find in powershell cmdB is started to run after cmdA finished, this behavior is different compare to CMD.exe, the two commands is expected to work simultaneously as producer-consumer. especially when the 1st one has a lot of messages to output, you want to look into the result at the first time rather than after the completion of the 1st cmd.

[Updated on 2016/08/09] after few more investigation, i guess the problem is because the pipeline operator '|' works different. this time i created a huge text file by copy&paste (~50MB) and used a same command line for both CMD and Powershell:

C:\tools\msys2\usr\bin\cat.exe .\WindowsUpdate.log | C:\tools\msys2\usr\bin\cat.exe

I used the cat command from msys2 and avoid the `type' command as they are different on both shells.

in CMD, messages are printed immediately but in Powershell i waited ~30 seconds to see messages come out. I tried about 10 times and got the same result. (PS: i'm not using a SSD). But if in Powershell i changed the receiver from `cat' to Out-Host, it works fine.

Yuwu
  • 1
  • 1
  • What is your question? Also, I doubt that the two commands should work as producer-consumer... – Martin Brandl Aug 08 '16 at 09:14
  • 3
    `more` is a command that does not implement per-line piping, so it can only wait for the entire output to accumulate. Use `ls -r | oh -p` (short for out-host -paging). See also [Equivalent of 'more' or 'less' command in Powershell?](http://stackoverflow.com/q/1078920) – wOxxOm Aug 08 '16 at 09:17
  • "you'll find in powershell cmdB is started to run after cmdA finished": This depends entirely on the implementation of both cmdA and cmdB, the statement cannot be said to be correct. – Chris Dent Aug 08 '16 at 09:27
  • @wOxxOm yes, the out-host works fine. I get a program written in C to read each line from stdin and output it, is it possible to have this kind of program to work for 'per-line piping'? – Yuwu Aug 08 '16 at 09:42
  • The thing is `ls` (actually Get-ChildItem) in powershell doesn't write lines, it produces objects whereas `more` expects one text object, not a sequence of them. So in your case I guess it depends on what reads/fills the pipe. In the worst case you'd have to use something like [SteppablePipeline](https://msdn.microsoft.com/en-us/library/system.management.automation.steppablepipeline(v=vs.85).aspx) (judging by my quick googling). – wOxxOm Aug 08 '16 at 09:55

0 Answers0