Use shebang/hashbang in Windows Command Prompt

北战南征 提交于 2019-11-28 06:16:44
a_horse_with_no_name

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.

Actually looks like someone who knows how to write batch files better than I has also approached this. Their batch file may work better.

http://whitescreen.nicolaas.net/programming/windows-shebangs

No, there's no way to "force" the command prompt to do this.

Windows simply wasn't designed like Unix/Linux.

Is there a shell extension that does something similar?

Not that I've heard of, but that should be asked on Super User, not here.

Here is a simple way to force windows to support shebang however it has a caveat regarding the file naming. Copy the following text in to a batch file and follow general idea in REM comments.

@echo off

REM This batch file adds a cheesy shebang support for windows
REM Caveat is that you must use a specific extension for your script files and associate that extension in Windows with this batch program.
REM Suggested extension is .wss (Windows Shebang Script)
REM One method to still easily determine script type visually is to use double extensions.  e.g.  script.pl.wss

setlocal enableextensions disabledelayedexpansion
if [%1] == [] goto usage

for /f "usebackq delims=" %%a IN (%1) do (
  set shebang=%%a
  goto decode_shebang
)

:decode_shebang
set parser=%shebang:~2%
if NOT "#!%parser%" == "%shebang%" goto not_shebang

:execute_script
"%parser%" %*
set exit_stat=%errorlevel%
echo script return status: %exit_stat%
goto finale

:not_shebang
echo ERROR script first line %shebang% is not a valid shebang
echo       maybe %1 is not a shebanged script
goto finale

:usage
echo usage: %0 'script with #! shebang' [scripts args]+
echo        This batch file will inspect the shebang and extract the
echo        script parser/interpreter which it will call to run the script

:finale
pause
exit /B %exit_stat%

There's no way to execute random file, unless it is an actual executable binary file. Windows CreateProcess() function just not designed for it. The only files it can execute are those with MZ magic or with extensions from %PATHEXT% list.

However, CMD itself has a limited support for custom interpreters through EXTPROC clause. The limitation is that interpreter should also support and omit this clause in its execution.

Command prompt does not support shebang , however there are a lot hybrid techniques for different languages that you allow to combine batch and other languages syntax in one file.As your question concerns node.js here's a batch-node.js hybrid (save it with .bat or .cmd extension):

0</* :{
        @echo off
        node %~f0 %*
        exit /b %errorlevel%
:} */0;


console.log(" ---Self called node.js script--- ");

console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));

It is possible to be done with many other languages like Ruby,Perl,Python,PHP and etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!