Cross platform script command line (e.g. .bat and .sh)

I noticed that Windows 7 allows you to execute .sh files as if they were .bat files. This made me wonder if the .sh file could be written in such a way that it could be run on Windows and Linux (say bash).

The first thing that comes to my mind is to fabricate an if-expression so that Windows and Ubuntu can handle it and go to the appropriate block to execute platform-specific commands. How can I do that?

Note. I know this is not a good practice. I also know that scripting languages ​​such as Python are much better suited to solve this problem than the command line mixed script syntax. I'm just curious...

+5
source share
2 answers

You can use this:

rem(){ :;};rem ' @goto b ';echo sh;exit :b @echo batch 

This is a valid script shell and batch, and will execute different blocks depending on how it is executed.

Change the echo and @echo lines to do what you want.

+7
source

AFAIK, you cannot directly run .sh files from Windows' cmd.exe . To do this, you will need a * nix emulation layer to run the shell. Check out Cygwin or Msys / MinGW

0
source

All Articles