Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use shebang/hashbang in Windows Command Prompt

I'm currently using the serve script to serve up directories with Node.js on Windows 7. It works well in the MSYS shell or using sh, as I've put node.exe and the serve script in my ~/bin (which is on my PATH), and typing just "serve" works because of it's Shebang (#!) directive which tells the shell to run it with node.

However, Windows Command Prompt doesn't seem to support normal files without a *.bat or *.exe extension, nor the shebang directive. Are there any registry keys or other hacks that I can get to force this behavior out of the built-in cmd.exe?

I know I could just write up a simple batch file to run it with node, but I was wondering if it could be done in a built-in fasion so I don't have to write a script for every script like this?

Update: Actually, I was thinking, is it possible to write a default handler for all 'files not found' etc. that I could automatically try executing within sh -c?

Thanks.

like image 772
Adam M-W Avatar asked Jul 25 '11 14:07

Adam M-W


People also ask

How do I run a shebang script?

The #! syntax is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The directive must be the first line in the Linux shell script and must start with shebang #! . You can add argument after the shebang characters, which is optional.

How do I use YES in CMD?

Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.

What is shebang command?

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark ( #!) at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.


1 Answers

Yes, this is possible using the PATHEXT environment variable. Which is e.g. also used to register .vbs or .wsh scripts to be run "directly".

First you need to extend the PATHEXT variable to contain the extension of that serve script (in the following I assume that extension is .foo as I don't know Node.js)

The default values are something like this:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC 

You need to change it (through the Control Panel) to look like this:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.FOO 

Using the control panel (Control Panel -> System -> Advanced System Settings -> Environment Variables is necessary to persist the value of the PATHEXT variable.

Then you need to register the correct "interpreter" with that extension using the commands FTYPE and ASSOC:

ASSOC .foo=FooScript FTYPE FooScript=foorunner.exe %1 %* 

(The above example is shamelessly taken from the help provided by ftype /?.)

ASSOC and FTYPE will write directly into the registry, so you will need an administrative account to run them.

like image 87
a_horse_with_no_name Avatar answered Oct 10 '22 21:10

a_horse_with_no_name