* How to do "ls /tmp > /dev/null" in Guile?
@ 2016-03-19 10:19 Alex Kost
2016-03-19 11:02 ` Marko Rauhamaa
0 siblings, 1 reply; 5+ messages in thread
From: Alex Kost @ 2016-03-19 10:19 UTC (permalink / raw)
To: guile-user
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?
--
Thanks,
Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to do "ls /tmp > /dev/null" in Guile?
2016-03-19 10:19 How to do "ls /tmp > /dev/null" in Guile? Alex Kost
@ 2016-03-19 11:02 ` Marko Rauhamaa
2016-03-20 8:32 ` Alex Kost
0 siblings, 1 reply; 5+ messages in thread
From: Marko Rauhamaa @ 2016-03-19 11:02 UTC (permalink / raw)
To: Alex Kost; +Cc: guile-user
Alex Kost <alezost@gmail.com>:
> 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))))
Marko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to do "ls /tmp > /dev/null" in Guile?
2016-03-19 11:02 ` Marko Rauhamaa
@ 2016-03-20 8:32 ` Alex Kost
2016-03-20 9:23 ` Marko Rauhamaa
0 siblings, 1 reply; 5+ messages in thread
From: Alex Kost @ 2016-03-20 8:32 UTC (permalink / raw)
To: Marko Rauhamaa; +Cc: guile-user
Marko Rauhamaa (2016-03-19 14:02 +0300) wrote:
> Alex Kost <alezost@gmail.com>:
>
>> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to do "ls /tmp > /dev/null" in Guile?
2016-03-20 8:32 ` Alex Kost
@ 2016-03-20 9:23 ` Marko Rauhamaa
2016-03-21 9:40 ` Alex Kost
0 siblings, 1 reply; 5+ messages in thread
From: Marko Rauhamaa @ 2016-03-20 9:23 UTC (permalink / raw)
To: Alex Kost; +Cc: guile-user
Alex Kost <alezost@gmail.com>:
> 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."
> ???)
Replace (close-input-port) with (close-pipe); that should give you the
exit status. Also you don't need to copy the data to a dummy port if you
only want to ignore it.
> and there would be no standard/error output from both "ls" calls. Is
> it possible?
The diagnostic output (stderr) is a different story. You want to be sure
to want to ignore it. To implement that properly, you should go
lower-level with operating system calls (fork, exec, waitpid). Simply
open "/dev/null" for writing and dup the file descriptor into the slots
1 and 2 of the child process.
Marko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to do "ls /tmp > /dev/null" in Guile?
2016-03-20 9:23 ` Marko Rauhamaa
@ 2016-03-21 9:40 ` Alex Kost
0 siblings, 0 replies; 5+ messages in thread
From: Alex Kost @ 2016-03-21 9:40 UTC (permalink / raw)
To: Marko Rauhamaa; +Cc: guile-user
Marko Rauhamaa (2016-03-20 12:23 +0300) wrote:
> Alex Kost <alezost@gmail.com>:
>
>> 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."
>> ???)
>
> Replace (close-input-port) with (close-pipe); that should give you the
> exit status. Also you don't need to copy the data to a dummy port if you
> only want to ignore it.
(close-pipe) is what I needed, thanks! For the record here is the procedure:
(define (system-no-output* . args)
"Like 'system*' but suppress the output of the command indicated by ARGS."
(let ((port (apply open-pipe* OPEN_READ args)))
(read-string port)
(close-pipe port)))
>> and there would be no standard/error output from both "ls" calls. Is
>> it possible?
>
> The diagnostic output (stderr) is a different story. You want to be sure
> to want to ignore it. To implement that properly, you should go
> lower-level with operating system calls (fork, exec, waitpid). Simply
> open "/dev/null" for writing and dup the file descriptor into the slots
> 1 and 2 of the child process.
Thanks for the info! There are things to think about.
--
Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-03-21 9:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-19 10:19 How to do "ls /tmp > /dev/null" in Guile? Alex Kost
2016-03-19 11:02 ` Marko Rauhamaa
2016-03-20 8:32 ` Alex Kost
2016-03-20 9:23 ` Marko Rauhamaa
2016-03-21 9:40 ` Alex Kost
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).