Search code examples
chicken-scheme

get output from system command in Chicken Scheme


How would I go about getting the output from a system command in Chicken Scheme?

Here's how I do I typically do it in NewLISP:

(nth 0 (exec "<COMMAND>")) 
;; the `(nth 0...` is just there 'cause I only care about the first element in 
;; the list returned by `exec`

Solution

  • The posix unit, built in to Chicken Scheme, has call-with-output-pipe. It can be combined with read-all from the utils unit (also built-in to Chicken Scheme) to read the output from a shell command:

    #;1> (use posix)
    #;2> (call-with-input-pipe "echo hello world" read-all)
    "hello world\n"
    

    http://wiki.call-cc.org/man/4/Unit%20posix#call-with-output-pipe

    http://wiki.call-cc.org/man/4/Unit%20utils#read-all