Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a REPL (read-eval-print-loop) for the Clojure programming language listening on a network socket. I can send it code over the network and the code will be evaluated and the answer sent back to me. I'm using the netcat utility to send the code

# send the contents of code.txt to port 5678 on somemachine
$ netcat somemachine 5678 <code.txt

My problem is that I don't want to use the Loop capability of this REPL. On my end, I want to issue a one-shot command, receive and show the answer on stdout, then exit my command (the REPL on the remote machine should stay up). The below example works except netcat doesn't exit and I remain connected to the REPL indefinitely.

$ cat code.txt
(doseq [x (range 0 10)] (println (* x x)))
$ netcat somemachine 5678 <code.txt
user=> 0
1
4
9
16
25
36
49
64
81
nil
user=>

We can see that the REPL's prompt ("user=> ") is displayed twice: once before the code is evaluated and once after. It seems like the key to getting what I want is chaining something on to the end of my command that watches and waits until the prompt is shown a second time then kills netcat.

When running this manually in a bash terminal, I could instead just press Ctrl-C after I see the second prompt, but the purpose of this is to be used in an extension to the TextMate editor.

Any ideas?

share|improve this question
    
For the Clojure-specific case, it looks like you can solve this problem inside out by appending (.close *in*) on to the code that is sent to the REPL. (via technomancy on #clojure). –  xyz Jul 28 '11 at 23:41
2  
I don't think netcat is the right tool here. You need to send output, then read input until you meet a prompt. This looks like a job for expect. –  Gilles Jul 29 '11 at 14:34

1 Answer 1

I think the other two answers are probably better for your specific case, but to answer your general question, you could tack this bit on the end:

netcat somemachine 5678 <code.txt |
    while read ln;do 
      [ "${ln#user=>}" != "$ln" -a "$gotone" = true ] && exit
      [ "${ln#user=>}" != "$ln"  ] && gotone=true
      echo "$ln"
    done
share|improve this answer

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.