stringschemeportchez-scheme

Capturing the output of an external call as a string (in Chez Scheme)


Using Chez Scheme, I tried to capture the output of an external command into a string port (see https://www.scheme.com/csug8/io.html):

(define output 
  (with-output-to-string (lambda () (system "ls -a")))
  )

The output of (system "ls -a") is displayed on the screen, and output is empty (tested as a script with chezscheme 9.5.4 on Linux).

The same code works correctly with Racket 8.3 [CS].


Solution

  • The following works with Chez Scheme 9.5.8 on Linux.

    (define (capture-standard-output command)
      (let-values ([(in out err pid) (open-process-ports command 'block
                                       (make-transcoder (utf-8-codec)))])
        (get-string-all out)))
    
    
    (capture-standard-output "ls -a")  ;; => ".\n..\nbar\nfoo\n"