Can anyone explain me about the sh in clojure in order to execute the system command?

筅森魡賤 提交于 2021-02-05 12:24:55

问题


I'm using Mac OS. I want to execute a system command using (use '[clojure.java.shell :only [sh]]), like in How to execute system commands?. I have read https://clojuredocs.org/clojure.java.shell/sh but wasn't able to understand things like how many parameters can be passed in one syntax, etc.

In windows I have tried (sh "cmd" "/C" "dir")and it was working but in Mac OS, how do I execute the above syntax? Moreover I want to pass more parameters than just dir. For example, I want to execute (sh "cmd" "/c" "mged" "test.g"). NOTE: mged and test.g are referred from Brl-Cad.

I want to draw a sphere using above example.


回答1:


On MacOS or Linux this works:

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



回答2:


clojure.java.shell just spawns the process with the given arguments (and :env and :dir and ... - see the doc). So first of all there is most likely no cmd on OSX/Unix, but there usually is a shell. And the "same" as cmd /c on the shell is -c. -c takes one argument and you can write your "shell code" there - that means you can use pipes, redirects, env-vars, ... - so if you just want to execute a tool with a param, use:

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

If you want "shell features" use:

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

(note that the "shell code" is just one argument)



来源:https://stackoverflow.com/questions/56772111/can-anyone-explain-me-about-the-sh-in-clojure-in-order-to-execute-the-system-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!