[英]Execute Batch file in Windows Subsystem for Linux
Is it possible to execute a .bat
file from a Windows Subsystem for Linux (eg. Ubuntu)?是否可以从适用于 Linux 的 Windows 子系统(例如 Ubuntu)执行.bat
文件?
It is trivial to run an .exe
file from a WSL, but I haven't found a trivial way to run a .bat
.从 WSL 运行.exe
文件很简单,但我还没有找到运行.bat
的简单方法。 The only way I found is to open cmd.exe
, but that is a bit cumbersome to do every time I need to execute a .bat
file.我发现的唯一方法是打开cmd.exe
,但每次我需要执行.bat
文件时都这样做有点麻烦。
Unfortunately at the moment you cannot do so without either using:不幸的是,目前你不能不使用:
cmd.exe /c foo.bat
…or the following hack using binfmt: ...或以下使用 binfmt 的 hack:
sudo sh -c "echo :WindowsBatch:E::bat::/init: > /proc/sys/fs/binfmt_misc/register"
You could then just type:然后你可以输入:
foo.bat
The problems with this method are that you'd need to be root, run it each time you opened a bash window, probably do the same for .cmd
files too and, I suppose, any bash script name ending with .bat
could have issues!这种方法的问题是你需要是 root,每次打开 bash 窗口时都运行它,可能对.cmd
文件也做同样的事情,我想,任何以.bat
结尾的 bash 脚本名称都可能有问题!
I guess until Microsoft deals with this issue, you're limited to the above.我想在 Microsoft 处理此问题之前,您仅限于上述内容。
If the first answer doesn't work, try:如果第一个答案不起作用,请尝试:
sudo sh -c "echo :WindowsBatch:E::bat::/init: > /proc/sys/fs/binfmt_misc/register"
Then, locate the file.然后,找到该文件。 Then make the file executable:然后使文件可执行:
chmod +x foo.bat
Then run the file:然后运行文件:
./foo.bat
If it says permission denied, just try running it with sudo如果它说权限被拒绝,请尝试使用 sudo 运行它
Sorry to post on such an old thread but I found a way around it (not sure if it's something that's changed from updates, I'm posting on 18/02/2021).很抱歉在这样一个旧线程上发帖,但我找到了解决方法(不确定它是否因更新而改变,我在 2021 年 2 月 18 日发帖)。 I found that running CMD and then feeding it arguments (to link to the requisite batch file) works - it also an accept constructed arguments sent to that command.我发现运行 CMD 然后给它提供参数(链接到必要的批处理文件)是有效的 - 它也是一个接受发送到该命令的构造参数。 So in my case (I'm using Debian 10.8 in WSL on 10 Pro):所以就我而言(我在 10 Pro 的 WSL 中使用 Debian 10.8):
/mnt/c/windows/system32/cmd.exe /mnt/c/temp/batchfile.bat "-arga -argb -argc"
I think this may be fairly consistent and workable workaround (just using C:\\temp\\bathfile.bat
as an example, just use the relative /mnt/<drv>/<dir>/<batchfile>
path for your needs).我认为这可能是相当一致且可行的解决方法(仅以C:\\temp\\bathfile.bat
为例,只需根据您的需要使用相对/mnt/<drv>/<dir>/<batchfile>
路径)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.