hi
how to exec shell command then collect result as a string? (like with perl $a=`ls`;) I need to call maple and collect the result. shell-command seems to return only exit status.
Thanks
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com
Afficher les réponses par date
There's open-process / close / process-status for handling the output as a port.
Using string ports you can copy the contents into a string:
(define substring-size 1024)
(define (port->string port) (define str (##make-string substring-size)) (call-with-output-string "" (lambda (out-port) (let lp () (let ((res (read-substring str 0 substring-size port))) (if (< res substring-size) (begin (##string-shrink! str res) (##write-string str out-port)) (begin (##write-string str out-port) (lp))))))))
Note that this stops reading if either end-of-file is reached or a timeout happens (in case you did set up one). Using peek-char and eof-object? you could check whether it stopped because of a timeout or eof.
(Using threads, you could capture stderr and stdout separately if process-port would be able to return separate ports for them.)
You could also slurp in the port as a stream (lazily built list), that can be beneficial if you don't want to have the whole content in memory at once, and can be more practical for working with parsing combinators, for example.
Christian.
On 5/27/07, Naruto Canada narutocanada@yahoo.ca wrote:
hi
how to exec shell command then collect result as a string? (like with perl $a=`ls`;) I need to call maple and collect the result. shell-command seems to return only exit status.
You could do something like:
(read-line (open-process "ls") #f)
Guillaume
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 27-May-07, at 3:31 PM, Guillaume Germain wrote:
You could do something like:
(read-line (open-process "ls") #f)
And if you need to interact several times with the program over stdin/ stdout you can do something like this:
(define bc (open-process "bc")) (display "1+2\n" bc) (force-output bc) (read-line bc)
"3"
(display "3*4\n" bc) (force-output bc) (read-line bc)
"12"
(close-port bc) (process-status bc)
0
"bc" is the Unix arbitrary precision calculator. If maple can also interact over stdin/stdout then this would be an appropriate interface.
If you want to do this with other Scheme implementations, check out the "processio" Snow package (http://snow.iro.umontreal.ca/? viewpkg=processio).
Marc