使用 gnu clisp 运行 shell 命令

发布于 2024-09-05 08:41:45 字数 825 浏览 15 评论 0原文

我正在尝试为 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 技术交流群。

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

发布评论

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

评论(4

一身仙ぐ女味 2024-09-12 08:41:45

像这样的东西吗?

版本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
..."

Something like this?

Version 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
..."
清音悠歌 2024-09-12 08:41:45

根据 关于 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 read
  • a pathname designator - writes to the designated file
  • nil - ignores the output

If 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 to run-program, you'll need to write to it yourself, copying the data from the input stream returned by run-program.

笑着哭最痛 2024-09-12 08:41:45

您具体询问的是关于 clisp 的问题。我会在这里添加
如果您使用 Clozure CL,那么您还可以轻松运行操作系统子进程。

一些示例:

;;; Capture the output of the "uname" program in a lisp string-stream
;;; and return the generated string (which will contain a trailing
;;; newline.)
? (with-output-to-string (stream)
    (run-program "uname" '("-r") :output stream))
;;; Write a string to *STANDARD-OUTPUT*, the hard way.
? (run-program "cat" () :input (make-string-input-stream "hello") :output t)
;;; Find out that "ls" doesn't expand wildcards.
? (run-program "ls" '("*.lisp") :output t)
;;; Let the shell expand wildcards.
? (run-program "sh" '("-c" "ls *.lisp") :output t)

在位于此处的 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:

;;; Capture the output of the "uname" program in a lisp string-stream
;;; and return the generated string (which will contain a trailing
;;; newline.)
? (with-output-to-string (stream)
    (run-program "uname" '("-r") :output stream))
;;; Write a string to *STANDARD-OUTPUT*, the hard way.
? (run-program "cat" () :input (make-string-input-stream "hello") :output t)
;;; Find out that "ls" doesn't expand wildcards.
? (run-program "ls" '("*.lisp") :output t)
;;; Let the shell expand wildcards.
? (run-program "sh" '("-c" "ls *.lisp") :output t)

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.

℡寂寞咖啡 2024-09-12 08:41:45

这是一个较短的

(defun system(cmd)
  (ext:shell (string cmd)))

> (system '"cd ..; ls -lrt; pwd")

This is a shorter one

(defun system(cmd)
  (ext:shell (string cmd)))

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