用gnu clisp运行shell命令 [英] running shell commands with gnu clisp

查看:86
本文介绍了用gnu clisp运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为像这样的剪辑创建一个系统"命令

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

我有这样的东西:

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

但是,我不确定如何将流转换为字符串.我已经对Hyperspec和Google进行了多次审查.

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:使用Ranier的命令并使用with-output-to-stream

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

然后尝试运行grep,它在我的路径中...

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

推荐答案

像这样吗?

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

这篇关于用gnu clisp运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆