How do I change directory in command line with Clojure?

怎甘沉沦 提交于 2020-01-03 07:18:38

问题


What I'm looking for is this kind of command line interaction at the Windows command line:

C:\temp1>clj some_script.clj
C:\temp2>

Where some_script.clj contains something like:

(cd "c:\\temp2")

So the question is - how do I implement the function cd? Have experimented with clojure.java.shell, but it does not look like the lib I need. This might be a simple question, the problem might be that I'm not fluent in Java?!


回答1:


You can't do this in Java, so you can't do it in Clojure. See Changing the current working directory in Java?




回答2:


clojure can do this. You only need to change a dynamic global variable called *sh-dir*. run the following code in your repl:

 (use '[clojure.java.sh])
 (sh "ls")
    => {:exit 0, :out "LICENSE\nREADME.md\nauto_deploy.iml\ndoc\nproject.clj\nresources\nsrc\ntarget\ntest\n", :err ""}

    (binding [*sh-dir* "c:/"] (sh "ls"))
{:exit 0,
 :out "$360Section
       $GetCurrent
       $Recycle.Bin
       Boot
       Documents and Settings
       ImbaMallLog.txt
       Intel
       MSOCache
       OEMSY
       PerfLogs
       Program Files
       Program Files (x86)
       ProgramData
       Python27
       Recovery
       System Volume Information
       Users
       Windows
       apache-ant-1.9.3
       bootmgr
       hiberfil.sys
       inetpub
       pagefile.sys
       ",
 :err ""}

see the doc for more info. you can use (alter-var-root #'clojure.java.shell/*sh-dir* (constantly "the-cd-path")) to change it constantly. Thanks for isaac telling me about this. Hope this helps.



来源:https://stackoverflow.com/questions/3921744/how-do-i-change-directory-in-command-line-with-clojure

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