guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to do "ls /tmp > /dev/null" in Guile?


From: Alex Kost
Subject: Re: How to do "ls /tmp > /dev/null" in Guile?
Date: Sun, 20 Mar 2016 11:32:11 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Marko Rauhamaa (2016-03-19 14:02 +0300) wrote:

> Alex Kost <address@hidden>:
>
>> Hello, in the guile REPL I evaluated the following:
>>
>>   (with-output-to-port (%make-void-port "w")
>>     (lambda () (display "foo") (newline)))
>>
>> and I got no output as expected.  Then I tried the following:
>>
>>   (with-output-to-port (%make-void-port "w")
>>     (lambda () (system* "ls" "/tmp")))
>>
>> but there was an output from "ls" command.  So my question is: how to
>> get rid of this output?
>
> When you use ports (which the operating system knows nothing about),
> Guile needs to actively jockey the data between them.
>
>     (use-modules (ice-9 popen))
>     (with-output-to-port (%make-void-port "w")
>       (lambda ()
>          (let ((output (open-input-pipe "ls /tmp")))
>            (let loop ()
>              (let ((c (read-char output)))
>                (if (not (eof-object? c))
>                    (begin
>                      (write-char c)
>                      (loop)))))
>            (close-input-port output))))

Ah, thanks!  I get it.  But I also want to check an exit status of the
running command (sorry, that I didn't mention it).  So I would like to
have the following procedure:

(define (system-no-output* . args)
  "Like 'system*' but suppress the output of the command indicated by ARGS."
  ???)

Or even better (it would be a perfect solution for me) the following macro:

(define-syntax-rule (with-no-process-output body ...)
  "Run BODY and suppress all output of the executed sub-processes."
  ???)

So that I could do something like this:

(with-no-process-output
  (let ((status1 (system* "ls" "/tmp"))
        (status2 (system* "ls" "/foo")))
    (format #t "Very useful info: ~a, ~a~%" status1 status2)))

and there would be no standard/error output from both "ls" calls.  Is it
possible?

-- 
Alex



reply via email to

[Prev in Thread] Current Thread [Next in Thread]