简体   繁体   English

如何使用Clojure在命令行中更改目录?

[英]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.

相关问题 如何在Windows命令行中复制目录及其文件夹? - How do I copy the directory and its folders in windows command line? 如何使用 Windows 命令行更改目录 - how to change directory using Windows command line 如何使用终端中的命令行将我的 https 更改为 http? - how do i change my https to http with a command line in terminal? 如何在命令行中更改python可执行文件的别名? - How do I change the alias of python executable in command line? 无论我在哪个目录,如何在Windows命令行中运行phpunit命令? - How do I run phpunit command in Windows command line no matter what directory I am in? 当我运行perl raku命令行程序时,lib目录出现在当前工作目录中 - 我该如何防止这种情况? - When I run a perl raku command line program a lib directory appears in the current working directory - how do I prevent this? 如何从命令行对目录进行这些更改? (RoR) - How do I make these changes to my directory from the command line? (RoR) 当从命令行使用它时,如何让依赖项将依赖项复制到lib目录? - How do I get Ivy to copy the dependencies to the lib directory when using it from the command line? 如何将文件移动到Windows命令行中文件创建日期的目录中? - How do I move files to a directory that's the create date of the files in Windows Command Line? 如何在我的终端中默认更改目录? (命令行) - How to change the directory by default in my Terminal? (command line)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM