【问题标题】:Can anyone explain me about the sh in clojure in order to execute the system command?谁能解释一下clojure中的sh以执行系统命令?
【发布时间】:2019-06-26 11:49:26
【问题描述】:

我使用的是 Mac OS。我想使用(use '[clojure.java.shell :only [sh]]) 执行系统命令,例如How to execute system commands?。我已阅读https://clojuredocs.org/clojure.java.shell/sh,但无法理解一种语法可以传递多少个参数等内容。

在windows中我试过(sh "cmd" "/C" "dir")它可以工作,但在Mac OS中,我该如何执行上述语法?此外,我想传递的参数不仅仅是dir。比如我要执行(sh "cmd" "/c" "mged" "test.g")。 注意:mgedtest.g 引用自 Brl-Cad

我想用上面的例子画一个球体。

【问题讨论】:

    标签: clojure clojure.spec clojure-core.logic


    【解决方案1】:

    在 MacOS 或 Linux 上有效:

    user=> (require '[clojure.java.shell :as sh])
    nil
    user=> (sh/sh "ls" "-la")
    ...
    

    【讨论】:

    • 很抱歉这么多天后回复您,感谢您的解释。这是我正在使用的代码 (ns vikram-cad.core (:require [clojure.java.shell :refer [sh]])) (defn example [] (println (:out (sh "mged" "test.g")))) 所以你能帮我创建一个任何名称的数据库,例如 test.g 和一个在 clojure 中带有 sh 函数的球体
    • 执行上述代码后,我收到此错误Execution error (IOException) at java.lang.ProcessImpl/forkAndExec (ProcessImpl.java:-2). error=2, No such file or directory
    【解决方案2】:

    clojure.java.shell 只是使用给定的参数生成进程(以及 :env:dir 和 ... - 请参阅文档)。所以首先在 OSX/Unix 上很可能没有cmd,但通常有一个 shell。而shell上与cmd /c“相同”的是-c-c 接受 one 参数,你可以在那里编写你的“shell 代码”——这意味着你可以使用管道、重定向、环境变量……——所以如果你只是想执行一个工具带参数,使用:

    (sh "mged" "test".g")
    

    如果您想要“外壳功能”,请使用:

    (sh "/bin/sh" "-c" "echo ${TERM} | tr x u")
    

    (注意“shell代码”只是一个参数)

    【讨论】:

    • 很抱歉这么多天后回复您,感谢@cfrick 的解释。所以现在我可以在 clojure 程序中使用 sh 函数,现在它也可以工作了,但是这个 (sh "mged" "test.g) 不起作用意味着它没有创建任何名称为 test.g 的数据库。这是我正在使用的代码(ns vikram-cad.core (:require [clojure.java.shell :refer [sh]])) (defn example [] (println (:out (sh "mged" "test.g")))) 所以你能帮我在 clojure 中使用 sh 函数创建一个数据库和一个球体
    • 如果您对问题有错误或进一步了解,请编辑您的问题。 sh 的结果很可能有一些有用的东西 - 所以不要只看:out
    • 在执行上述代码时,我收到此错误Execution error (IOException) at java.lang.ProcessImpl/forkAndExec (ProcessImpl.java:-2). error=2, No such file or directory
    • 那么很可能mged 不在你的路径中
    • 但是我已经添加了路径。 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:Applications/BRL-CAD\ \:\ MGED\ 7.24.0.app/Contents/Resources/rel-7.24.0/bin:/opt/X11/bin
    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2015-12-21
    • 2020-06-08
    相关资源
    最近更新 更多