what script (like .bat) would run on windows, mac, and linux
Asked Answered
S

8

7

I have a bat script that I'd like to make work on windows/Mac/linux, but I read .bat is only for windows. How can I make this script work on Mac and linux, or what type of script can I write that would work on all 3.

Sturgeon answered 16/11, 2011 at 18:50 Comment(1)
Check this answer for some syntactic tricks which might get you to where you want; https://mcmap.net/q/46240/-single-script-to-run-in-both-windows-batch-and-linux-bashShemikashemite
H
4

If you don't want to step up to a language like C, I would suggest Python as a multi-platform scripting language. It's easy to learn and has lots of generic, multi-platform libraries and functions.

You'll need to install it on Windows, and maybe OS X as well. But it's not difficult in any way.

Hiding answered 16/11, 2011 at 18:54 Comment(3)
So does that mean that when I distribute that script to the user, I have to ask them to install python? That's a lot more trouble than what I'm aiming for.Sturgeon
Yes. But like everyone else has said, there isn't a straight up scripting language that will work out of the box on every platform. You'd need to use something more cross platform like C or C++ or Java, and then distribute platform-specific executables.Hiding
every version of linux and osx already has python installed. They would probably have to install it on windows but you could also package it as an exe using py2exe so the user wouldn't have to install the python runtime.Eran
G
5

No such beast. Windows doesn't support the unix-style shell scripts that Mac/Linux use, and Mac/Linux don't have command.com/cmd.exe needed to support .bat files.

There's ways to fake this, like running Wine on mac/linux and cygwin on Windows, but then that's a whole other ball of wax.

Gymnastics answered 16/11, 2011 at 18:54 Comment(0)
H
4

If you don't want to step up to a language like C, I would suggest Python as a multi-platform scripting language. It's easy to learn and has lots of generic, multi-platform libraries and functions.

You'll need to install it on Windows, and maybe OS X as well. But it's not difficult in any way.

Hiding answered 16/11, 2011 at 18:54 Comment(3)
So does that mean that when I distribute that script to the user, I have to ask them to install python? That's a lot more trouble than what I'm aiming for.Sturgeon
Yes. But like everyone else has said, there isn't a straight up scripting language that will work out of the box on every platform. You'd need to use something more cross platform like C or C++ or Java, and then distribute platform-specific executables.Hiding
every version of linux and osx already has python installed. They would probably have to install it on windows but you could also package it as an exe using py2exe so the user wouldn't have to install the python runtime.Eran
H
3
  1. Python
  2. Bash + MSYS on Windows
  3. Groovy / Beanshell / Javascript ... with corresponding interpreter
Holcman answered 16/11, 2011 at 18:54 Comment(1)
Windows already includes a Javascript interpreter. It's part of Windows Script Host. en.wikipedia.org/wiki/Windows_Script_HostEasterly
S
2

There isn't one shell scripting language that is available on all three without installing something on one of the platforms. And in the case of Windows, installing something like Bash which is available on Linux and OSX, is less than useful as it never will integrate with Windows as seemlessly as it doesn on OSX and Linux, and never be able to do everything as integrated as a .bat or PowerShell file will be able to.

Personally when I need cross platform scripting I reach for Python.

Sabaean answered 16/11, 2011 at 18:53 Comment(2)
I wouldn't say installing Bash on Windows is useless. Installing a true native port would be a problem because it wouldn't mask things like path separator character differences, availability of external tools, etc. That's where Cygwin comes in, which does mask all this. Granted, this is at quite a large cost in terms of complexity and installation footprint, but having paid that cost, what you have is far from useless.Oligarchy
@WarrenYoung I didn't say useless I said less than useful. I have adding clarification on why that is.Sabaean
E
1

.bat scripts are only going to work on windows. I would keep the bat script that you have for windows and create a bash script designed to work on linux and osx.

Please don't make me install the cygwin tools just to run a script.

Eran answered 18/11, 2011 at 13:59 Comment(0)
E
0

you can write a batch-file and let it interpret from cmd on windows and sh on linux

but there is not much syntax in common, so you will be forced to run os-specific scripts from that point

Eversole answered 18/11, 2011 at 14:3 Comment(0)
G
0

If you're comfortable with JavaScript, you could install Node.js on each platform, then write a command line tool and run like so:

> npm install yourtool-cli -g
> yourtool [args]

N.B. it's common to add suffix -cli to your command line node package name. The -g option is to install globally rather than in the local folder.

The File System module lets you do a lot of stuff that batch/script files do, for example.

Gujarati answered 10/12, 2015 at 10:26 Comment(0)
A
0

You can install Powershell on Mac. In this case, you can write your ps1 files and execute them in Windows and Mac

Ase answered 15/10, 2024 at 0:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.