使用 gnu clisp 运行 shell 命令
我正在尝试为 clisp 创建一个像这样工作的“系统”命令
(setq result (system "pwd"))
;;now result is equal to /my/path/here
我有这样的东西:
(defun system (cmd)
(ext:run-program :output :stream))
但是,我不确定如何将流转换为字符串。我已经多次查看了 hyperspec 和 google。
编辑:使用 Ranier 的命令并使用 with-output-to-stream,
(defun system (cmd)
(with-output-to-string (stream)
(ext:run-program cmd :output stream)))
然后尝试运行 grep
,它在我的路径中......
[11]> (system "grep")
*** - STRING: argument #<OUTPUT STRING-OUTPUT-STREAM> should be a string, a
symbol or a character
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
Break 1 [12]> :r2
I'm trying to create a "system" command for clisp that works like this
(setq result (system "pwd"))
;;now result is equal to /my/path/here
I have something like this:
(defun system (cmd)
(ext:run-program :output :stream))
But, I am not sure how to transform a stream into a string. I've reviewed the hyperspec and google more than a few times.
edit: working with Ranier's command and using with-output-to-stream,
(defun system (cmd)
(with-output-to-string (stream)
(ext:run-program cmd :output stream)))
And then trying to run grep
, which is in my path...
[11]> (system "grep")
*** - STRING: argument #<OUTPUT STRING-OUTPUT-STREAM> should be a string, a
symbol or a character
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
Break 1 [12]> :r2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西吗?
版本2:
Something like this?
Version 2:
根据 关于
run-program
的 CLISP 文档,:output
参数应该是:terminal
之一 - 写入终端:stream
- 创建 和返回一个输入流,您可以从中读取nil
- 忽略输出如果您想要将输出收集到字符串中,您必须使用读写复制循环将数据从返回的流传输到字符串。您已经有
with-output-to-string
< /a> 正在运行,根据 Rainer 的建议,但您不需要将该输出流提供给run-program
,而是需要自己写入,从输入流复制数据< /em> 由run-program
返回。Per the CLISP documentation on
run-program
, the:output
argument should be one of:terminal
- writes to the terminal:stream
- creates and returns an input stream from which you can readnil
- ignores the outputIf you're looking to collect the output into a string, you'll have to use a read-write copying loop to transfer the data from the returned stream to a string. You already have
with-output-to-string
in play, per Rainer's suggestion, but instead of providing that output stream torun-program
, you'll need to write to it yourself, copying the data from the input stream returned byrun-program
.您具体询问的是关于 clisp 的问题。我会在这里添加
如果您使用 Clozure CL,那么您还可以轻松运行操作系统子进程。
一些示例:
在位于此处的 CCL 文档中搜索运行程序: http://ccl .clozure.com/ccl-documentation.html
在这个 stackoverflow 答案中,有一些很好的 Lisp 方法可以做到这一点: 进行系统调用,将 stdout 输出作为字符串返回 Rainer 再次来救援。谢谢拉尼尔。
You are asking specifically about clisp. I'll add here that
if you are using Clozure CL then you can also easily run os subprocesses.
Some examples:
Do a search for run-program in the CCL docs located here: http://ccl.clozure.com/ccl-documentation.html
There are a couple nice Lisp ways of doing this in this stackoverflow answer: Making a system call that returns the stdout output as a string Once again, Rainer to the rescue. Thanks Ranier.
这是一个较短的
This is a shorter one