Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm writing SML programs, and I'd like a way to quickly see the output from running a program in the REPL without actually running the REPL (to quickly see if a program has syntax errors - I plan to use this as a make program for .sml files in vim to view the output inside vim).. Currently, I have this:

sml file.sml | echo -e "\004"

So it runs the program, and then echoes Ctrl-D to exit the REPL. The problem is that its too quick to send the Ctrl-D key, so there is no output. I tried this too:

sml file.sml | sleep 2 ; echo -e "\004"

But that isn't doing it either. Any ideas on how I can get a dump of the output from the REPL?

share|improve this question
2  
You might wand to look at Expect –  glenn jackman Sep 30 '12 at 11:58
add comment

1 Answer

No, the problem is that you are misunderstanding how pipes work.

In Unix pipelines, data flows from left to right (the same direction as written text in English) – the first program's output becomes the second program's input. But the pipes are unidirectional; the second program's output is not sent to the first program.

When you run sml file.sml | echo -e "\004", you aren't echoing Ctrl+D to sml. Instead, you are piping sml's output to the echo command – where it gets discarded, since echo does not use stdin.

  1. keyboard → sml file.smlecho -e "\004" → screen

Your second attempt, sml file.sml | sleep 2 ; echo -e "\004", has a similar problem – the output of sml is sent to sleep (which also discards the input it receives). However, there is another problem: the echo command now isn't part of the pipeline at all; you could say that | has a higher precedence than ;. (You can group commands using parentheses; e.g. a | (b; c; d).)

  1. keyboard → sml file.smlsleep 2 → screen
  2. keyboard → echo -e "\004" → screen

As already said above, both attempts are written in the wrong direction. If you wanted to send the \004 character to sml, the pipeline should have been written like this:

echo -e "\004" | sml file.sml

in which, sml's output goes to the screen, where it should.

There is another problem, however. You are also confusing the CtrlD keypress with the "end of input" event.

The translation of CtrlD into "end of input" only happens at terminal device layer – in other words, only when you enter the Ctrl+D character into your terminal window. However, when you use echo -e "\004" in a pipe, it is not automagically translated to "end of input"; instead, the actual byte 004 [octal] is written.

If you want to actually tell the program that there is no more input, then do not write anything. Use a command that outputs nothing – for example, echo -n or true, or just redirect input from the /dev/null file.

echo -n | sml file.sml

sml file.sml < /dev/null
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.