12

在 clisp 中,以下代码有效:

(defun hit-history () (shell "tail ssqHitNum.txt"))

但是,在 Clozure CL 中,shell不支持该功能!

4

6 回答 6

10

不,没有标准的方法,但是有一些库为重要的实现提供了这个功能。例如,Quicklisp中有 trivial-shell 可用,它提供shell-command. (我实际上并没有测试它,但它是CLiki推荐的库之一。)还有外部程序。更新:这些天似乎更喜欢劣质外壳,正如 Ehvince 在评论和他自己的回答中指出的那样。

您还可以使用读取时条件来使不同的实现使用它们各自的功能来执行此操作。

CCL 有ccl:run-program,例如:

CL-USER> (run-program "whoami" '() :output *standard-output*)
foobar
#<EXTERNAL-PROCESS (whoami)[NIL] (EXITED : 0) #xC695EA6>
于 2011-10-11T04:24:09.197 回答
8

是的, UIOP 是ASDF的一部分,应该包含在所有现代实现中。

  • 同步命令:uiop:run-program
  • 异步命令:uiop:launch-program

所以例如

(uiop:run-program (list "firefox" "http:url") :output t)

或者

(defparameter *shell* (uiop:launch-program "bash" :input :stream :output :stream))

您可以在其中发送输入和读取输出。

他们在这里得到更多解释:  https ://lispcookbook.github.io/cl-cookbook/os.html#running-external-programs

trivial-shell已被弃用并替换为lower -shell,它在内部使用可移植的uiop run-program同步),所以我们可以使用它。

于 2016-12-26T21:26:39.727 回答
2
(defun dot->png (fname thunk)
   (with-open-file (*standard-output*
       fname
       :direction :output
       :if-exists :superseded)
     (funcall thunk))
   (ccl:run-program "dot" (list "-Tpng -O" fname))
)

当我学习 lisp p123 的土地时,我在 ccl(clozure) 中取得了成功

于 2012-08-12T00:33:43.390 回答
1

下面显示了从 common lisp 中调用 wget 的示例:

https://diasp.eu/posts/1742240

这是代码:

(sb-ext:run-program "/usr/bin/wget" '("-O" "<path-to-output-file>" "<url-link>") :output *standard-output*) 
于 2014-04-09T22:00:08.290 回答
1

看看inferior-shell包装。

(通过全能的quicklisp包管理器获取它。)

如果您有互联网,这适用于口译员:

(require 'inferior-shell)
(inferior-shell:run/s '(curl icanhazip.com))
于 2015-04-28T23:11:43.707 回答
0

CL21 定义了简单的方法:

(in-package :cl21-user)
(use-package :cl21.process)

然后使用run-process或使用 #` 阅读器宏:

(run-process '("ls" "-l"))
;-> total 0
;   drwxrwxrwt    5 root         wheel   170 Nov  1 18:00 Shared
;   drwxr-xr-x+ 174 nitro_idiot  staff  5916 Mar  5 21:41 nitro_idiot
;=> #<PROCESS /bin/sh -c ls -l /Users (76468) EXITED 0>

或者

#`ls -l /Users`
;=> "total 0
;   drwxrwxrwt    5 root         wheel   170 Nov  1 18:00 Shared
;   drwxr-xr-x+ 174 nitro_idiot  staff  5916 Mar  5 21:41 nitro_idiot
;   "
;   ""
;   0

源代码显示了 SBCL 和 CCL 的实现。

于 2017-07-23T05:38:15.603 回答