lisp - 使用 gnu clisp 运行 shell 命令

标签 lisp stream clisp

我正在尝试为 clisp 创建一个像这样工作的“系统”命令

(setq result (system "pwd"))

;;now result is equal to /my/path/here

我有这样的东西:

(defun system (cmd)
 (ext:run-program :output :stream))

但是,我不确定如何将流转换为字符串。我已经多次查看 hyperspec 和谷歌。

编辑:使用 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

最佳答案

是这样的吗?

版本 2:

(defun copy-stream (in out)
   (loop for line = (read-line in nil nil)
         while line
         do (write-line line out)))

(defun system (cmd)
  (with-open-stream (s1 (ext:run-program cmd :output :stream))
    (with-output-to-string (out)
      (copy-stream s1 out))))


[6]> (system "ls")
"#.emacs#
Applications
..."

关于lisp - 使用 gnu clisp 运行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3019142/

相关文章:

scheme - 传递 bool 函数的正确方法

c# - 使用 BinaryFormatter 序列化和反序列化 List<List<object>>

common-lisp - 学习 clisp 正则表达式

macros - Lisp 评估宏表达式中的变量

lisp - Common-LISP Print 函数本身

list - lisp 中以特定方式显示列表的函数

lisp - 从列表中提取列表

java - 如何合并两个具有相同键的嵌套映射并保留值

c++ - 如何在 C++ 中将输入流的前缀复制到不同的流?

lisp - 在 lisp SLIME 调试器中检查变量