I'm updating the test suite of the standard library of the Crystal programming language to run on Windows. To test the spawning of new processes with pipes for stdout and stdin on unix, cat is used as a "dummy process" which copies stdin to stdout for testing.
What's the simplest command to achieve the same effect on windows using cmd.exe commands?
A simple command that can be found in all windows versions that can be used as an alternative to the linux cat command in the indicated scenario is
find /v ""
As is, it will read lines from standard input and write them to standard output.
note: While more and findstr "^" could, sometimes, be used, both have several limitations that make find /v "" a better alternative.
Here is my solution:
powershell.exe -c "[Console]::OpenStandardInput().CopyTo([Console]::OpenStandardOutput())"
The accepted solution of using (find /v "") has some problems:
cat will return an exit code of 0 when the input is empty. A test which tests the exit code and expects it to be zero will fail when the input is empty. A workaround is to do find /v "" & : (assuming you are running it from cmd.exe, such as by using cmd.exe /c) – but then, if the find actually fails to run for any reason (see next bullet point), you will not get a failure exit code to notify youfind in your path before Windows find. (Of course, in that case you probably have cat in your path too, and hence don't need to do this at all – but, you may be looking for a solution which you know will work even on systems without MSYS/Cygwin/etc installed.) Assuming you are running it using cmd.exe /c, you can solve this by using %windir%\system32\find.exe /v ""I think this PowerShell oneliner is better in that it avoids the above problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With