Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading the output of another program

1 view
Skip to first unread message

Rodrigo Cunha

unread,
Oct 9, 2002, 9:24:03 PM10/9/02
to
Hi!

I'm trying to get the output of another program from within cmucl, in a
similar way to

print `/bin/ls`;

in perl...

The following is the result of my frustrated attempts until now:

(defun teste000 ()
(print (with-output-to-string (*standard-output*)
(run-program "/bin/ls" nil
:wait nil
:pty *standard-output*
:input nil
:error nil
))))

(teste000)

The funtion loads, executes without error, but produces "" as output...

Sugestions?

--
Rodrigo

Vassil Nikolov

unread,
Oct 10, 2002, 1:34:59 AM10/10/02
to
On Thu, 10 Oct 2002 02:24:03 +0100, Rodrigo Cunha <rn...@netcabo.pt> said:

[...]
RC> (defun teste000 ()
RC> (print (with-output-to-string (*standard-output*)
RC> (run-program "/bin/ls" nil
RC> :wait nil
RC> :pty *standard-output*
RC> :input nil
RC> :error nil
RC> ))))

RC> (teste000)

RC> The funtion loads, executes without error, but produces "" as output...

Don't you have to call PROCESS-CLOSE (since :PTY is non-NIL)?

Otherwise, did you check the value returned by RUN-PROGRAM to make
sure it does not fail to fork the child process? Did you try
calling RUN-PROGRAM with :OUTPUT <pathname> or :OUTPUT T to make
sure that the child can run and produce output correctly? (And you
do have files in the current directory, don't you?)

---Vassil.

--
Non-googlable is googlable.

Johan Ur Riise

unread,
Oct 13, 2002, 3:25:37 AM10/13/02
to
Rodrigo Cunha <rn...@netcabo.pt> writes:

> Hi!
>
> I'm trying to get the output of another program from within cmucl, in
> a similar way to
>
> print `/bin/ls`;

This is what I do:

(defun shell-command (command)
"Execute command in a shell, return a list of resulting lines"
(let (result (s (process-output
(run-program
"/bin/sh"
(list "-c" command)
:output :stream))))
(do ((l #1=(read-line s nil 'eof) #1#))
((eq l 'eof) "Reached end of file.")
(push l result))
(close s)
(reverse result)))

>
> Sugestions?


--
Johan Ur Riise
90 15 77 78

Johan Ur Riise

unread,
Oct 13, 2002, 1:27:26 PM10/13/02
to
Johan Ur Riise <r...@rsc.no> writes:


> This is what I do:
>
> (defun shell-command (command)
> "Execute command in a shell, return a list of resulting lines"
> (let (result (s (process-output
> (run-program
> "/bin/sh"
> (list "-c" command)
> :output :stream))))
> (do ((l #1=(read-line s nil 'eof) #1#))
> ((eq l 'eof) "Reached end of file.")
> (push l result))
> (close s)
> (reverse result)))

Following up to my own post, I would like to elaborate:

You could also run a program like ls directly (run-program "/bin/ls"
(list "-ltr") ...)

I notices afterwards that it is probably smart to use the
keyword-parameter :wait nil, else run-program will sometimes stop,
possibly because a filled up buffer.

run-program returns a process, I use process-output to extract the
output stream from that, and read until end of file, then close the
stream. Reading the user guide again, I see that it is even smarter to
close the process using process-close.

In addition to the cmucl user guide, there is a source-file named
run-program that could be helpful.

Fabricio Chalub

unread,
Oct 13, 2002, 9:28:01 PM10/13/02
to
Rodrigo Cunha <rn...@netcabo.pt> wrote in message news:<ao2kri$isnpd$1...@ID-160389.news.dfncis.de>...

> Hi!
>
> I'm trying to get the output of another program from within cmucl, in a
> similar way to
>
> print `/bin/ls`;
>

CLOCC/PORT's shell.lisp[1] has a PIPE-INPUT function that "Return an
input stream from which the command output will be read." and is
extremely useful and portable across several Lisp implementations.
This is a simple example, that only cares about reading the first
line, but you should get the idea:

(with-open-stream (s (port:pipe-input "uptime"))
(print (read-line s)))

1. http://clocc.sourceforge.net/dist/port.html

Cheers,
Fabricio
--
http://raw.no-ip.com/~fc/

0 new messages