0

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].

AlQuemist
  • 1,110
  • 3
  • 12
  • 22

2 Answers2

0
(define output
  (get-string-all (car (process "ls -a"))))

seems to work on MacOS

mnemenaut
  • 684
  • 2
  • 11
  • This works also under Linux. Yet, the difference between `system` and `process` is that "the `system` procedure waits for the process to exit before returning, ... while the `process` procedure returns immediately without waiting for the process to exit." – AlQuemist Mar 26 '22 at 21:38
  • Is there a way to wait for the process to exit (like what `system` does)? – AlQuemist Mar 26 '22 at 21:38
  • `open-process-ports` ? (wait for end-of-file?) – mnemenaut Mar 27 '22 at 06:56
  • Could you please explain, with code, how to start the process and how to wait for the `EOF`? – AlQuemist Mar 27 '22 at 14:22
0

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"

Peter Winton
  • 574
  • 3
  • 8