在 Linux 环境中运行 .bat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12680998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:29:03  来源:igfitidea点击:

Run .bat in Linux environment

windowslinuxbashbatch-filesh

提问by Sqeezer

Is there any way to run .bat file in Linux environment? I have a couple of .bat files, the purpose of them is to call mvn install:install-file scripts. Thus nothing OS dependent is in the scripts.

有没有办法在 Linux 环境中运行 .bat 文件?我有几个 .bat 文件,它们的目的是调用 mvn install:install-file 脚本。因此,脚本中没有任何依赖于操作系统的内容。

Thank you,

谢谢,

采纳答案by favoretti

You can use wineor dosbox, but in general there is no known batinterpreter for linux. There are, however, implementations of various unix shells for windows, there's even a standard toolkit, Windows Services for UNIX(a.k.a. SUA), which include kshimplementation and many other nice goodies, so if you want it OS-transparent, you could consider using that and write your scripts in a POSIX-compliant shell scripting language.

您可以使用winedosbox,但一般来说bat,Linux没有已知的解释器。然而,有各种适用于 Windows 的 unix shell 的实现,甚至还有一个标准工具包Windows Services for UNIX(又名SUA),其中包括ksh实现和许多其他不错的东西,所以如果你想要它对操作系统透明,你可以考虑使用它并编写你的脚本在符合 POSIX 标准的 shell 脚本语言中。

--- edit --- On the other hand, if your script contains nothing elseother than an mvn <params>, you can just make sure the file has execute permissions (xflag), prepend it with a shell interpreter (like /bin/bash script.bat) and have a go at it. Success not guaranteed, though.

---编辑---在另一方面,如果你的脚本包含没有别的比其他的mvn <params>,你可以确保文件具有执行权限(x标志),在前面加上它带有命令解释程序(像/bin/bash script.bat),在一展身手它。但是,不能保证成功。

回答by Dan

No. The batfiles are windows shell scripts, which probably execute windows commands and expect to run in a windows environment. You need to convert them to shell scripts in order to run them on linux, as your bash shell can not understand dos commands. Luckily, if the install file scripts are truly platform-independent, that should be easy. If you show an example, we may be able to help you translate.

不是。这些bat文件是windows shell 脚本,它可能执行windows 命令并期望在windows 环境中运行。您需要将它们转换为 shell 脚本才能在 linux 上运行它们,因为您的 bash shell 无法理解 dos 命令。幸运的是,如果安装文件脚本真正独立于平台,那应该很容易。如果您展示一个示例,我们可能会帮助您翻译。

回答by Chad Modad

The simple answer is yes there is a way to run it on Linux as long as:

简单的答案是肯定的,只要满足以下条件,就可以在 Linux 上运行它:

  • The commands you are running from the .batfile are in the $PATHon your Linux box
  • You are not using Microsoft specific BATCH file commands or control structures
  • 您从该.bat文件运行的命令位于$PATHLinux 机器上的
  • 您没有使用 Microsoft 特定的 BATCH 文件命令或控制结构

You will need to make the file executable and most likely prepend the contents of the file with a line that tells Linux which shell to run the script with. Something like this for bash: #!/bin/bash

您需要使文件可执行,并且很可能在文件内容前添加一行,告诉 Linux 使用哪个 shell 运行脚本。像这样的 bash:#!/bin/bash

回答by seguya

You could write the equivalent of your .bat script as a shell script.

您可以将 .bat 脚本的等价物编写为 shell 脚本。

回答by Oleg Abrazhaev

Install dosbox

安装 dosbox

sudo apt install dosbox

sudo apt install dosbox

Run it with dosbox

运行它 dosbox

Mount your home folder from your Linux os. Type inside dosbox

从您的 Linux 操作系统挂载您的主文件夹。在 dosbox 中输入

MOUNT D /home/<your user>

MOUNT D /home/<your user>

Switch folder drive

切换文件夹驱动

D:

D:

Now if you have a file called my.batin your user home directory, then inside doxbox just run it MY.BAT

现在,如果您my.bat在用户主目录中调用了一个文件,则在 doxbox 中运行它 MY.BAT

回答by Md Ashraful Islam

On Linux Terminal type

在 Linux 终端类型上

wine cmd

After that windows cmd will be played on your terminal. Go to the folder where your .bat file is located and type the bat files name and press enter. It will successfully run.

之后,windows cmd 将在您的终端上播放。转到 .bat 文件所在的文件夹并键入 bat 文件名,然后按 Enter。它将成功运行。