[racket] Embedding multiline shell scipts
On Sun, 6 Feb 2011 16:06:19 +0200
"Jukka Tuominen" <jukka.tuominen at finndesign.fi> wrote:
>
> So, once i have created the script within racket as multiline string,
> how do I actually run it within racket without writing or reading any
> external files, and capture the output of the script for further
> processing (also in racket).
>
> In other words, I could write the script into a file and run it with
> system/output, but how to do it without an external file?
>
Hmmm, one possibility might be to use process which nicely captures
output. process is especially good if you have a large amount of output
as you can process the output (e.g. write it to a log file) line by
line.
Example: Here the output of the command(s) will just be echoed to
stdout and to stderr.
(define (print-output in)
(let loop ((line (read-line in)))
(if (eof-object? line)
#t
(begin (printf "~a~n" line)
(loop (read-line in))))))
(define shell-cmd
(lambda (cmd)
(printf "~a~n" cmd) ;echoing the cmd which could be omitted
(let* ([p (process cmd)]
[stdout (first p)]
[stdin (second p)]
[stderr (fourth p)]
[ctrl (fifth p)])
(print-output stdout)
(print-output stderr)
(close-input-port stdout)
(close-output-port stdin)
(close-input-port stderr))))
(define test-cmd "
SOMEVAR=\"some-content\"
echo $SOMEVAR
")
(shell-cmd test-cmd)
I don't know how to merge stdout and stderr in proper sequence. People
who know racket better than me may interfere to improve my example.
--
Manfred