如何在 WSL 内部的 emacs 中运行命令(cmd.exe)?

如何在 WSL 内部的 emacs 中运行命令(cmd.exe)?

现在,当我在 Windows 10 上打开 WSL(Unbuntu)时,VcXsrv 下会启动 emacs。如果我调用“shell”,它会很好地运行 bash shell。但是,我有几个喜欢运行的命令文件。特别是,我想在类似 dos 的环境中运行 Windows Studio 命令行工具,因此首先运行“c:\program files (x86)\microsoft visual studion...\vsvars.bat”,然后运行我的“xyzzy.cmd”脚本,该脚本调用 nmake 来构建和运行旧式测试程序。

我尝试直接从 bash 运行这两个程序,但它们不起作用(这并不奇怪)。然后我尝试在 emacs 中调用 c:\windows...\cmd.exe 作为我的 shell 解释器,但这似乎没有出现提示。它只是挂起了 shell。我还尝试了在 windows 上直接运行 emacs 时使用的“cmdproxy.exe”,但效果并没有好转。

目前,我在 Windows 上直接运行一个 emacs(如果我理解正确的话,当我在那里调用 shell 时,它会调出命令(使用 cmdproxy.exe)。在那里我可以运行脚本,emacs 会将输出捕获到缓冲区中,然后我可以将其写入文件。我有上面描述的单独 emacs 来进行实际编辑,然后我在那里读取文件。我宁愿只有一个 emacs,这样我就可以使用 emacs 函数来切换缓冲区等。

(顺便说一句,我想我可以在 cygwin 下做到这一点,但我宁愿使用 WSL。)

如果我在 emacs 中打开一个 shell 窗口并在其中输入 cmd.exe 的路径($ 是我的 shell 提示符),将发生以下情况:

$ ls -l  /mnt/c/Windows/System32/cmd.exe
-r-xr-xr-x 2 cfclark cfclark 280064 Oct 28 23:03 /mnt/c/Windows/System32/cmd.exe
$ /mnt/c/Windows/System32/cmd.exe /?
[6n

此时 shell 已挂起。

但是你的回答给了我一个想法,我更接近了:

$ /mnt/c/Windows/System32/cmd.exe /C z:/testbase/cpandrun.cmd -next -rev24 | cat
The system cannot find the path specified.
 +----------------------------------------------------------------------
  Setting up environment vars to build the tutorials with MVC++ 2019
 +----------------------------------------------------------------------
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\cfclark\AppData\Roaming
...  (lots of environment variables omitted)
The system cannot find the path specified.
yxx1.exe does not exist
Too many of the files: yxx.exe and yxxdiff.cmd 
         or yxx1.exe, yxx2.exe, and yxx.cmd.cmd
are missing from \yxx.24\bin.
The system cannot find the file specified.
The system cannot find the path specified.
Done!
The system cannot find the path specified.
Done!

.cmd 文件预计从合并目录运行,以便它看起来像分区的根目录,但我可能可以在 .cmd 脚本中修复它。

答案1

为了从 emacs 内部(wsl 内部)完成这项工作,您需要在以下行中添加“| cat”:

/mnt/c/Windows/System32/cmd.exe /c path_to_run.cmd | cat

这会让输出显示在 emacs shell 缓冲区中,它甚至会等待您在缓冲区中输入的输入。至少它对我的 .cmd 文件中的“pause”命令是这样的。

相关内容