【发布时间】:2014-04-21 22:19:25
【问题描述】:
如何从特定目录执行 shell 命令(例如,git gui)?我更喜欢一种不依赖于&& 或; 等shell 运算符的跨平台方法,因为我需要它在Windows 和unix 上运行。 (例如,调用 cd /path/to/dir && git gui 在 Windows 上不起作用,因为 && 无效。)
我试过了:
(async-shell-command (concat "cd \""
(replace-regexp-in-string
"/" "\\\\" (file-name-directory (buffer-file-name))) "\" ; git gui"))
这失败了,因为无论出于何种原因,shell 都认为 ; git gui 是路径的一部分,并报告:The system cannot find the path specified.
所以,我宁愿不处理 shell 怪癖,我希望有一个 elisp 函数可以为 shell-command 或 async-shell-command 设置目录。我试过了:
(shell-process-pushd (file-name-directory (buffer-file-name)))
(shell-command "git gui")
(shell-process-popd nil)
这没有效果:git gui 总是在我的主目录中打开。 (另外,shell-process-pushd/popd 没有记录。)
这也行不通:
(start-process "gitgui" nil "git" "gui")
这也不行:
(let '(bufdir (file-name-directory (buffer-file-name)))
(with-temp-buffer
(print bufdir)
(cd bufdir)
(shell-command "git gui")))
【问题讨论】: