[racket] Embedding multiline shell scipts

From: Thomas Chust (chust at web.de)
Date: Sun Feb 6 15:37:12 EST 2011

2011/2/6 Jukka Tuominen <jukka.tuominen at finndesign.fi>:
> [...]
> I slightly modified the Manfred's code to capture the output
> [...]

Hello,

the code seems somewhat more complicated than necessary to me and it
lacks some cleanup and error handling. I would suggest to use the
available utilities from racket/port to simplify the I/O code and the
control procedure returned from process to at least wait for the
external process to finish.

A module like this should do the trick:

  #lang racket/base
  (require
   racket/match
   racket/port
   racket/system)

  (define (shell command)
    (match-let* ([(list stdout stdin pid stderr control) (process command)]
                 [lines (port->lines (merge-input stdout stderr))])
      (close-output-port stdin)
      (close-input-port stdout)
      (close-input-port stderr)
      (control 'wait)
      (case (control 'status)
        [(done-ok)
         lines]
        [(done-error)
         (error
          'shell "~e failed with exit code ~a"
          command (control 'exit-code))])))

  (provide
   (all-defined-out))

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


Posted on the users mailing list.