2

我正在编写一个需要处理命令输出的通用 lisp 程序。但是,当我尝试在另一个函数中使用结果时,我只得到一个 NIL 作为返回值。

这是我用来运行命令的函数:

(defun run-command (command &optional arguments)
       (with-open-stream (pipe 
                 (ext:run-program command :arguments arguments
                                  :output :stream :wait nil)) 
       (loop
                :for line = (read-line pipe nil nil)
                :while line :collect line)))

其中,当它自己运行时会给出:

CL-USER> (run-command "ls" '("-l" "/tmp/test"))
         ("-rw-r--r-- 1 petergil petergil 0 2011-06-23 22:02 /tmp/test")

但是,当我通过函数运行它时,只返回 NIL:

(defun sh-ls (filename)
       (run-command "ls" '( "-l" filename)))
CL-USER> (sh-ls  "/tmp/test")
         NIL

如何在我的函数中使用结果?

4

2 回答 2

7

试试这个:

(defun sh-ls (filename)
       (run-command "ls" (list "-l" filename)))

'("-l" filename) 引用列表和符号'filename',而不是评估文件名。

于 2011-06-23T21:11:07.673 回答
4

您还可以在 sexpr 和文件名之前使用反引号 ` 来评估它:

(defun sh-ls (filename)
       (run-command "ls" `("-l" ,filename)))
于 2011-06-24T12:02:09.793 回答