Batch Files as Shell Scripts Revisited

 

Four years ago I wrote about a method that could be used to have the Unix Bourne shell interpret Windows batch files. I'm using this trick a lot, because programming using the Windows/DOS batch files facilities is decidedly painful, whereas the Bourne shell remains a classy programming environment. There are still many cases where the style of Unix shell programming outshines and outperforms even modern scripting languages.

The solution I came up involves starting each batch file that contains a Unix shell script with the following sequence:

@#!sh %0.bat %*
For this to work you must have in your path a Unix-compatible shell named sh, and the batch file named #!sh.bat. (Download the file by right-clicking on the link; note that the file name starts with #!).

I had to rewrite the original #!sh.bat helper code implementation, because nowadays Cygwin complains when it sees a Windows-style path. Therefore, the helper code converts the batch file's Windows path into the Cygwin style, before passing it to the Unix shell. While at it I also removed a limitation of the original implementation that restricted the command line arguments to ten.

If you think that I'm unfairly criticizing batch file programming, have a look at the code to see the contortions I had to go through in order to perform a fairly simple task. On the other hand, this is the only batch file I'll ever need to write. Enjoy!

Comments   Toot! Share


Last modified: Wednesday, August 4, 2010 11:21 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.