10

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.

Thank you,

Sqeezer
  • 1,267
  • 2
  • 14
  • 23

8 Answers8

16

You can use wine or dosbox, but in general there is no known bat interpreter 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 ksh implementation 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.

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

favoretti
  • 29,299
  • 4
  • 48
  • 61
  • 1
    Thanks for the answer. The script uses 'set', 'rem', 'call' commands. The ones are not recognized in Unix environment. So sad that such interpreter has not been created yet. – Sqeezer Oct 02 '12 at 06:13
  • The big gotcha here is that Windows batch files likely use CRLF as line delimiters, which bash tends to choke on in bizarre ways. Use [`dos2unix`](http://en.wikipedia.org/wiki/Unix2dos) to convert from CRLF to CR format. – Digital Trauma Feb 11 '14 at 18:34
  • 1
    Small correction - there are `bat` interpreters for linux: winescript – ensonic Jun 25 '23 at 14:24
  • @ensonic you're completely correct, nowadays there are various interpreters available, the original answer stems from over 10 years ago :) – favoretti Aug 03 '23 at 13:52
4

On Linux Terminal type

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.

  • I think this can also be executed as `wine cmd /c `, to automatically close the `cmd` window after execution is completed. I haven't tested it as I am not on a Linux machine at the moment. It might actually just hide the `cmd` window completely. – AntumDeluge Jun 18 '22 at 20:05
3

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

  • The commands you are running from the .bat file are in the $PATH on your Linux box
  • You are not using Microsoft specific BATCH file commands or control structures

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

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
Chad Modad
  • 31
  • 1
  • The big gotcha here is that Windows batch files likely use CRLF as line delimiters, which bash tends to choke on in bizarre ways. Use [`dos2unix`](http://en.wikipedia.org/wiki/Unix2dos) to convert from CRLF to CR format. – Digital Trauma Feb 11 '14 at 18:34
0

Install dosbox

sudo apt install dosbox

Run it with dosbox

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

MOUNT D /home/<your user>

Switch folder drive

D:

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

Oleg Abrazhaev
  • 2,751
  • 2
  • 28
  • 41
0

You can run any batch file easily in linux with notepad++, you can find notepad++ in any linux app store. I had downloaded it from snap store which is for linux. Notepad++ have a option named run, it will run the batch file for you on any environment

Nobody
  • 3
  • 3
0

Contrary to what others said, there is at least one interpreter for .bat files on linux: http://dcjtech.info/topic/winescript/

ensonic
  • 3,304
  • 20
  • 31
-1

No. The bat files 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.

Dan
  • 10,531
  • 2
  • 36
  • 55
-2

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

seguya
  • 56
  • 2