简体   繁体   English

在适用于 Linux 的 Windows 子系统中执行批处理文件

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

相关问题 我的 Windows 10 告诉我的 Linux 子系统通过批处理文件或类似文件运行某些东西的方法? - A way for my Windows 10 to tell my Linux subsystem to do run something via a batch file or similar? Flutter 和 Windows Linux 的子系统 - Flutter and Windows Subsystem for Linux 在 VS Code 中使用 Windows Linux 子系统 bash 时出现“没有这样的文件或目录” - "No such file or directory" when using Windows Linux Subsystem bash with VS Code 如何从WSL(Linux的Windows子系统)访问aws配置文件? - How to access aws config file from WSL (Windows subsystem for Linux)? 如何在Windows的Linux子系统(WSL)下通过bash从R执行linux命令? - How to execute linux commands from R via bash under the Windows Subsystem for Linux (WSL)? 在适用于Linux的Windows子系统上安装Apache - Installing Apache on Windows Subsystem for Linux 是否存在适用于Linux的Windows子系统的便携式版本 - Is there a portable version of the Windows Subsystem for Linux 在带有 Linux 子系统的 Windows 上取消共享文件夹 - Unshare a folder on Windows with Linux subsystem 在Windows批处理文件中执行命令 - Execute command in Windows batch file 在Windows 7 Remotly上执行批处理文件? - Execute Batch file on Windows 7 Remotly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM