1

What is the equivalent of the Unix echo foo | cat?

ECHO foo | TYPE CON hangs, waiting for input, at least on Windows XP/SP3. Possibly CON is not stdin but keyboard input.

You may wonder what is the point of this exercise: There are programs which behave differently when they notice that their output is piped, and I want a way to test them.

feklee
  • 7,555
  • 9
  • 54
  • 72
  • What are you trying to accomplish? `echo` isn't the same as **stdin**, and is already directed to **stdout** by definition, so the pipe to `cat` is redundant in this case. – Dan Bechard Sep 13 '13 at 13:42
  • @Dan Just updated the question with info about the *why*. – feklee Sep 13 '13 at 13:55
  • Can you give an example? – foxidrive Sep 13 '13 at 13:57
  • @foxidrive: Quote from [Node.js documentation](http://nodejs.org/api/stdio.html#stdio_console): *"The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when it's a pipe (to avoid blocking for long periods of time)."* – feklee Sep 13 '13 at 13:59
  • Dan's question - What are you trying to accomplish? will help define the actual problem, to see if there is a workaround for it. – foxidrive Sep 13 '13 at 14:04
  • @foxidrive As I write in my question, I want to test how applications behave when their output destination is a pipe. – feklee Sep 13 '13 at 14:06
  • @feklee In that case, `foo.exe | more` should work just fine on Windows. – Dan Bechard Sep 13 '13 at 14:08
  • @feklee How does the app know it's output is being piped? Does it check for _any_ redirection? If so, see my edit below. – Dan Bechard Sep 13 '13 at 14:17
  • @Dan Don't ask me how. But `Node.js` programs indeed differentiate between a file (`>`) and a pipe (`|`) as destination for output. – feklee Sep 13 '13 at 14:26

2 Answers2

3

Unsure what you want to do but this may help:

type file|more

And this may be more appropriate for your needs.

foo.exe | findstr "^"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • 1
    `echo foo | more` would be the closest alias, but I'm still not sure what his goal is. – Dan Bechard Sep 13 '13 at 13:45
  • `MORE` is OK for me. It's not perfect though, since it pages, which I neither need nor want. Therefore I'll keep this question open for now, hoping for a better solution. – feklee Sep 13 '13 at 14:23
  • I like the findstr solution a lot! – Dan Bechard Sep 13 '13 at 15:02
  • +1, FINDSTR is the better option, as MORE will convert tabs into spaces. I believe MORE will eventually hang if the input is large enough. – dbenham Sep 13 '13 at 15:59
1

As I understand it, you are looking for an application you can pipe to which simply passes anything piped to it through to stdout.

I believe foo.exe | more will serve your purpose on Windows.

Note: more does have the side effect of paging the output, so if you need to test longer outputs you could write a simple application which does the redirection.

Edit: You can write a simple batch file to redirect stdin to stdout and pipe to that. From jeb's answer here:

@echo off
setlocal DisableDelayedExpansion

for /F "tokens=*" %%a in ('findstr /n $') do (
  set "line=%%a"
  setlocal EnableDelayedExpansion
  set "line=!line:*:=!"
  echo(!line!
  endlocal
)

Save this as redir.bat and use like so foo.exe | redir.bat. Tested on Win7. Compatible with default Windows install. Only downside is it's not an easy one-liner to remember.

I would use more for simple cases, and fall back on this for longer outputs.

Community
  • 1
  • 1
Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
  • `foo.exe | tee` will provide an unpaged output - if he selects an appropriate third party tee filter – foxidrive Sep 13 '13 at 14:14
  • Indeed I have `tee` installed, from [GnuWin](http://gnuwin32.sourceforge.net/). I was hoping for something included in Windows. The programs I am testing (based on Node.js) detect `>` and `|` as different. – feklee Sep 13 '13 at 14:22
  • `foo.exe |repl "(.*)" "$1"` may work, where repl is a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855 – foxidrive Sep 13 '13 at 14:28
  • good plan. The question was confusing so thought patterns were jumbled - you can just use `foo.exe | findstr "^"` – foxidrive Sep 13 '13 at 14:49