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

后端 未结 2 969
一向
一向 2021-01-29 06:25

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://cl

相关标签:
2条回答
  • 2021-01-29 07:12

    On MacOS or Linux this works:

    user=> (require '[clojure.java.shell :as sh])
    nil
    user=> (sh/sh "ls" "-la")
    ...
    
    0 讨论(0)
  • 2021-01-29 07:13

    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)

    0 讨论(0)
提交回复
热议问题