Create multi-platform #CLI script for #Windows, #Linux & #MacOSX
Do you need to make run script which will work on multiple platforms? You can use my idea. In my case it’s run script which runs Java application and you can found it here. Interested in how it works?
How it works?
It is based on different line-braking between Windows and Unix and bash presence on Unix systems.
It also expects that Unix systems has not GOTO
command and don’t stop on error.
#!/bin/bash
^M
GOTO Windows^M
# Unix
ls
exit
^M
:Windows^M
dir^M
exit^M
The file must contains these parts:
#!/bin/bash
for Unix to determine how to execute this file- An empty line for Windows to explode lines
GOTO Windows
for Windows to skip Unix lines- An Unix code and
exit
- An empty line for Windows to explode lines
:Windows
anchor for Windows- A Windows code and
exit
Don’t forget that Unix lines must end with \n
, but Windows lines must end with \n\r
(^M
in example).