Running Windows Batch Files on Linux

D

DevynCJohnson

Guest
I have noticed that many people on the Internet ask about running Windows batch files on Linux. Unfortunately, many of the answers such people receive state that batch scripts cannot run on Linux. However, Linux users can indeed run batch files. Also, Windows batch files can be run on Windows like a native shell script.

First, the Linux system must have Wine installed. On most Linux systems, this requires the user to install the "wine" package. On some Debian-based systems, users may have conflicts with the "nvidia-libopencl1-331-updates" package. This can be fixed by installing "ocl-icd-opencl-dev" and its dependencies. The package manager should remove "nvidia-libopencl1-331-updates", if not, then the user must do so. If for some reason, Wine is not found in the repos, then either add this PPA (ppa:ubuntu-wine/ppa) or go to https://www.winehq.org/ and download the needed package.

Once Wine is installed, open a terminal and run "wineconsole cmd". This will open a separate Windows-Console window. From here, users can run various Windows terminal commands. Batch files can be run by typing "start FILENAME.bat".

linux-starting-wineconsole.png


wine-console.png


Alternately, type "wine cmd" to run the Windows-Console in the Linux terminal.
cmd-in-terminal.png


When in the native Linux shell, the batch files can be executed by typing "wine cmd.exe /c FILENAME.bat" or any of the following ways.
wine cmd < FILENAME.bat
wine cmd.exe < FILENAME.bat
wine start FILENAME.bat
wine cmd /c FILENAME.bat

In the user's bashrc file (~/.bashrc), it may help to add the following lines for Linux shell scripting purposes.
Code:
export WINEPREFIX=$HOME/.wine/
export WINEHOME=$HOME/.wine/
# for 32-bit
export WINEARCH=win32
# for 64-bit
export WINEARCH=win64

At this point, many Linux users may find it difficult to need to open a terminal and run that command just to run a batch file. Thankfully, there are two ways to fix that problem.

The quickest fix is to add the line seen below to the alias file (~/.bash_aliases). Next, logout and log back in so that the alias file will re-initialize. Then, users can type "winbatch FILENAME.bat" to execute a batch file in a terminal.
Code:
alias winbatch="wine cmd /c "

The second alternative is to make batch files execute as if they were a native shell script. To do so, download Winescript at http://dcjtech.info/wp-content/uploads/2015/02/winescript-v2015.02.08.zip. Next, uncompress the file and be sure that the file is named "winescript". Then, with Root privileges, move the script to /usr/share/bin/ or /usr/bin/ by typing "mv ./winescript /usr/share/bin/winescript" in a terminal. Finally, make the file executable by running "chmod +x /usr/share/bin/winescript" with Root privileges.

If Winescript and Wine have been installed successfully, then the user must make two changes to batch files to make them act like native scripts. First, make "#!/usr/bin/env winescript" the first line (hashpling) in the batch file. Lastly, make the batch file executable by running "chmod +x ./FILENAME.bat". Now, the user can execute this Windows batch file natively on GNU/Linux.

winescript.png


Now that batch files can be used on Linux, it may help to know some general facts. "*.bat" is the extension that was first used in DOS. Later, Windows NT and OS/2 added "*.cmd". Some shells (like 4OS2) supported the "*.btm" extension.

Further Reading
http://en.wikipedia.org/wiki/Batch_file
 

Attachments

  • slide.jpg
    slide.jpg
    39 KB · Views: 118,759


Thanks for the useful article, it worked. Just to put it in a simple step by step manner for laymen Ubuntu users like me:

1. Install Wine & configure it to relevant Windows version if needed

2. Open Gedit (Ubuntu's Notepad)>Edit>Preferences>Plugins>select 'External tools execute external commands shell scripts'>Close

3. Go to your .bat file>right click>Properties>Permissions>select 'allow executing file as program'>Close

4. Double-click the .bat file, it will open in gedit>Tools>External Tools>Run command>in the popped up box type 'wineconsole cmd' (without inverted commas)>OK.

5. A Wine console terminal will pop up, in it type 'start yourfilename.bat' and hit enter.
 
Last edited:

Members online


Top