[英]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: 我正在寻找的是Windows命令行中的这种命令行交互:
C:\temp1>clj some_script.clj
C:\temp2>
Where some_script.clj contains something like: some_script.clj包含以下内容:
(cd "c:\\temp2")
So the question is - how do I implement the function cd? 所以问题是 - 我如何实现功能cd? Have experimented with clojure.java.shell, but it does not look like the lib I need.
已经尝试过clojure.java.shell,但它看起来不像我需要的lib。 This might be a simple question, the problem might be that I'm not fluent in Java?!
这可能是一个简单的问题,问题可能是我不能流利使用Java?!
You can't do this in Java, so you can't do it in Clojure. 你不能用Java做到这一点,所以你不能在Clojure中做到这一点。 See Changing the current working directory in Java?
请参阅使用Java更改当前工作目录?
clojure can do this. clojure可以做到这一点。 You only need to change a dynamic global variable called
*sh-dir*
. 您只需要更改名为
*sh-dir*
的动态全局变量。 run the following code in your repl: 在您的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. 有关详细信息,请参阅doc。 you can use
(alter-var-root #'clojure.java.shell/*sh-dir* (constantly "the-cd-path"))
to change it constantly. 你可以使用
(alter-var-root #'clojure.java.shell/*sh-dir* (constantly "the-cd-path"))
来不断改变它。 Thanks for isaac telling me about this. 感谢isaac告诉我这件事。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.