1

IS there any way to make Windows scripts (BAT) files run on a Unix machine? Like importing a function or a library for example, which is only recognised on the Unix OS, but has no effect when run in Windows.

OR it could be possible to write both sets of commands in the same file and do something like this:

If OS = 'Windows' then execute between lines A and B
else execute between lines C and D
5
  • I think the second method is probably better since any misinterpretation of characters like '/' or '\' can be avoided.
    – Jay
    Feb 5, 2013 at 7:04
  • 2
    .bat or .cmd Windows batch files are really meant for Windows only. Though you could possibly run (*)sh scripts on Windows via cygwin, you should consider using something else (like Python?)
    – Alvin Wong
    Feb 5, 2013 at 7:07
  • 2
    what exactly are you trying to accomplish?
    – Keltari
    Feb 5, 2013 at 15:30
  • @AlvinWong But python programs require python to be installed :(
    – Jay
    Feb 7, 2013 at 3:24
  • @Keltari I am trying to make a database health check program that performs certain tasks and produces a report with the results of each task.
    – Jay
    Feb 7, 2013 at 3:26

2 Answers 2

1

You can't natively run a BAT script on UNIX/Linux. But you could try Wine:

Wine (originally an acronym for "Wine Is Not an Emulator") is a compatibility layer capable of running Windows applications on several POSIX-compliant operating systems.

You can download Wine installers for several Linux and BSD distros and for Solaris here (you will need to compile the source for other UNIX flavors, though).

Once Wine is installed and configured, copy the BAT script to your UNIX/Linux computer and run it like this (from http://forum.winehq.org/viewtopic.php?t=8795):

# wine cmd < path/to/your/file.bat
# wine start path/to/your/file.bat
# wine cmd /c path/to/your/file.bat

Notice that though Wine will do its best to execute the scripts there are portability issues like filesystem case-sensitivy, path names, text encoding issues, etc. that may break things.

That's why I'd recommend that you use Wine for legacy scripts. New scripts are best written in a platform independent language, like Perl or Python.

1
  • wine wouldn't be an option, since my script wouldn't only run on designated machines. I may or may not have admin rights to install wine anyway.
    – Jay
    Feb 7, 2013 at 3:27
0

There are plenty of alternative scripting languages that work on Windows, UNIX, Linux, as well as other OSs.

Perl would probably be the best choice. Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl can do just about anything OS related that you would want, not to mention Comprehensive Perl Archive Network (CPAN). CPAN is an archive of over 25,000 distributions of software written in Perl, as well as documentation for it. There are "plugins" for just about everything you could imagine. Integration to all the major databases, popular software products, and much more.

Python, Ruby, Lua, are other examples.

Another possible solution would be to run UNIX/Linux scripts on Windows. This can be accomplished with Cygwin. In short, Cygwin is a collection of tools which provide a Linux look and feel environment for Windows.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .