[plt-scheme] reading the output of a shell command execution

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Feb 8 10:50:58 EST 2005

On Feb  8, Pedro Kroger wrote:
> I'm running a shell program using "process/ports" and trying to read
> it's output using a string port "port". If I type the body of the
> following function in the REPL I get some results (e.g. "No
> changes!\n") but if I put the code in a function "foo" it returns
> "".
> 
> (define (foo)
>   (define port (open-output-string))
>   (current-directory "bib")
>   (process/ports port #f #f "darcs whatsnew")
>   (current-directory "/home/kroger")
>   (get-output-string port))
> 
> I'm pretty new to scheme and would appreciate any advice.

The process is running asynchronously -- when you typed stuff on the
REPL you probably gave it enough time to finish, but in a function,
you run the process and immediately get its output before it had some
chance to generate some.  Your two options are:

1. Use the value that process/ports return (see the help desk entry),
   it contains a control procedure which you should use with 'wait,
   seomthing like this:

     ((list-ref (process/ports port #f #f "darcs whatsnew") 4) 'wait)

2. Use `system' which executes the command synchronously, but you'll
   need to use parameterize:

     (parameterize ([current-output-port port])
       (system "darcs whatsnew"))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.