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
From: Vassil Nikolov
Subject: Re: reading the output of another program
Date:
Message-ID: <uptui501o.fsf@poboxes.com>
On Thu, 10 Oct 2002 02:24:03 +0100, Rodrigo Cunha <····@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.
Rodrigo Cunha <····@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 <·@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.
--
Johan Ur Riise
90 15 77 78
Rodrigo Cunha <····@netcabo.pt> wrote in message news:<··············@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/