简体   繁体   中英

How do I change directory in command line with Clojure?

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?!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM