Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I noticed that Windows 7 enables to execute .sh files as if they were .bat files. That got me wondering whether it is possible to write a .sh file such that it can be executed in Windows and Linux (let's say bash).

The first thing that comes to my mind is to fabricate an if-statement such that Windows and Ubuntu can deal with it and jump into the according block to execute plattform-specific commands. How could this be done?

Note: I know this is not good practice. I also know that scripting languages like Python are far better suited to solve this problem than a mixed-syntax command line script would be. I'm just curious...

like image 846
wehnsdaefflae Avatar asked Apr 30 '15 11:04

wehnsdaefflae


People also ask

What are .sh and .bat files?

bat) The command line utilities that control the Launcher (launcher.sh on UNIX operating systems and launcher. bat on Windows operating systems) can be configured specifically for running systems in multiple Launcher processes.

Is .bat and .sh file same?

bat file is a windows batch file it contains a sequence of windows/dos commands. These cannot be used in a Unix shell prompt. A . sh file is a unix shell script it contains a series of unix commands.

Can you run .sh in CMD?

Type 'sh' in cmd window to redirect into Bourne shell and run your commands in terminal. Git Bash comes with Git for Windows.

Are shell scripts cross-platform?

the scripts can be executed cross platform and be executed easily. easy to setup.


1 Answers

You could use this:

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

It's valid shell script and batch, and will execute different blocks depending on how it's run.

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

like image 152
Biffen Avatar answered Sep 29 '22 11:09

Biffen