Common Lisp 中是否存在运行外部程序的标准方法?

发布于 2024-12-09 07:07:33 字数 155 浏览 1 评论 0原文

在 clisp 中,以下代码有效:

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

但是,在 Clozure CL 中,不支持 shell 函数!

In clisp, the following code works:

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

However, in Clozure CL, the shell function is not supported!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

じее 2024-12-16 07:07:33

不,没有标准的方法,但是有一些库为重要的实现提供了此功能。例如,Quicklisp 中提供了 trivial-shell,它提供了 shell-command 。 (我实际上并没有测试它,但它属于推荐库之一href="http://www.cliki.net/index" rel="nofollow noreferrer">CLiki。)还有外部程序。更新:正如 Ehvince 指出的那样,inferior-shell 现在似乎更受欢迎在评论和他自己的回答中。

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

CCL有ccl:run-program,例如:

CL-USER> (run-program "whoami" '() :output *standard-output*)
foobar
#<EXTERNAL-PROCESS (whoami)[NIL] (EXITED : 0) #xC695EA6>

No, there is no standard way, but there are libraries which provide this functionality for the important implementations. For example, there's trivial-shell available in Quicklisp, which provides shell-command. (I didn't actually test it, but its among the recommended libraries on CLiki.) There is also external-program. Update: inferior-shell seems to be prefered these days, as Ehvince points out in a comment and his own answer.

You could also use read-time conditionals to make different implementations use their respective functionality to do this.

CCL has ccl:run-program, for example:

CL-USER> (run-program "whoami" '() :output *standard-output*)
foobar
#<EXTERNAL-PROCESS (whoami)[NIL] (EXITED : 0) #xC695EA6>
金兰素衣 2024-12-16 07:07:33

是的,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 已弃用并替换为 inferior-shell,内部使用可移植的 uioprun-program (同步),所以我们可以使用它。

Yes, with UIOP, part of ASDF, that should be included in all modern implementations.

  • synchronous commands: uiop:run-program
  • asynchronous commands: uiop:launch-program

So for example

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

or

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

where you can send input and read output.

They are more explained here: https://lispcookbook.github.io/cl-cookbook/os.html#running-external-programs

trivial-shell is deprecated and replaced by inferior-shell, which internally uses the portable uiop's run-program (synchronous), so we can use just that.

何以心动 2024-12-16 07:07:33
(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)中运行成功

(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))
)

i run success in ccl(clozure),when study land of lisp p123

看透却不说透 2024-12-16 07:07:33

下面显示了从 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*) 

The following shows an example of calling wget from within common lisp:

https://diasp.eu/posts/1742240

Here's the code:

(sb-ext:run-program "/usr/bin/wget" '("-O" "<path-to-output-file>" "<url-link>") :output *standard-output*) 
小糖芽 2024-12-16 07:07:33

看一下 inferior-shell 包。

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

如果您有互联网,这可以在解释器中运行:

(require 'inferior-shell)
(inferior-shell:run/s '(curl icanhazip.com))

Have a look at the inferior-shell package.

(Get it via the almighty quicklisp package manager.)

This works in the interpreter, if you have internet:

(require 'inferior-shell)
(inferior-shell:run/s '(curl icanhazip.com))
谷夏 2024-12-16 07:07:33

CL21 定义了简单的方法:

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

然后使用 run-process 或使用 #` reader 宏:

(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 的实现。

CL21 defines simple methods:

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

Then either with run-process or with the #` reader macro:

(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>

or

#`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

The source shows implementation for SBCL and CCL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文