[racket] Embedding multiline shell scipts
Hi all,
I slightly modified the Manfred's code to capture the output (see below). It
seems to do the job, but I haven't tried other cases yet or error handling,
but it's good to continue from here. Thanks Manfred!
Thank you everybody else, as well! I appreciate the shell scripting isn't
the optimal way of doing things. I also treat it as a temporary solution,
and I'll only use it in places I have to. But for now, it allows me to move
on and do things I couldn't otherwise.
br, jukka
(require racket)
(
(lambda ()
(define output '())
(define (print-output in)
(let loop ((line (read-line in)))
(if (eof-object? line)
#t
(begin (set! output (append output (list line)))
;(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)
output)))
(define test-cmd "
SOMEVAR=\"some-content\"
echo $SOMEVAR
ls
")
(shell-cmd test-cmd))
)
>> ("dir1" "dir2" "file1" "file2)
> -----Original Message-----
> From: users-bounces at racket-lang.org
> [mailto:users-bounces at racket-lang.org]On Behalf Of Manfred Lotz
> Sent: 06 February 2011 16:26
> To: users at racket-lang.org
> Subject: Re: [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
> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users