在 Windows 命令提示符中使用 shebang/hashbang

发布于 2024-11-26 08:39:44 字数 699 浏览 1 评论 0原文

我目前正在使用 serve 脚本来提供目录 Node.js 在 Windows 7 上。它在 MSYS shell 或使用 sh 中运行良好,因为我已经放置了 node.exe 和服务脚本在我的〜/ bin(在我的路径上)中,然后输入“serve”之所以有效,是因为它是 Shebang (#!) 指令告诉 shell 用 node 运行它。

但是,Windows 命令提示符似乎不支持没有 *.bat 或 *.exe 扩展名的普通文件,也不支持 shebang 指令。是否有任何注册表项或其他黑客可以让我从内置 cmd.exe 中强制执行此行为?

我知道我可以编写一个简单的批处理文件来使用节点运行它,但我想知道它是否可以以内置方式完成,这样我就不必为每个脚本编写一个脚本?

更新:实际上,我在想,是否可以为所有“未找到文件”等编写一个默认处理程序,以便我可以在 sh -c 内自动尝试执行?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

以往的大感动 2024-12-03 08:39:44

是的,使用 PATHEXT 环境变量可以实现这一点。例如,它还用于注册要“直接”运行的.vbs.wsh 脚本。

首先,您需要扩展 PATHEXT 变量以包含该服务脚本的扩展名(在下面我假设扩展名是 .foo,因为我不知道 Node.js)

默认值类似于这个:

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

您需要将其更改为如下所示(通过控制面板):

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

使用控制面板(控制面板 -> 系统 -> 高级系统设置 -> 环境变量对于保留 PATHEXT 变量的值是必要的 。

那么你需要注册正确的 使用命令 FTYPEASSOC 具有该扩展名的“解释器”:(

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

上面的示例无耻地取自 ftype /? 提供的帮助。 )

ASSOC 和 FTYPE 将直接写入注册表,因此您需要一个管理帐户来运行它们。

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.

清音悠歌 2024-12-03 08:39:44

命令提示符不支持 shebang ,但是有很多针对不同语言的混合技术,您可以将批处理和其他语言语法组合在一个文件中。由于您的问题涉及 node.js,这里有一个批处理-node.js 混合(将其保存为.bat.cmd 扩展名):

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));

可以使用许多其他语言(如 Ruby、Perl、Python、PHP 等)来完成。

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.

夜未央樱花落 2024-12-03 08:39:44

这是强制 Windows 支持 shebang 的简单方法,但它有一个关于文件命名的警告。将以下文本复制到批处理文件中,并遵循 REM 注释中的总体思路。

@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%

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%
只是我以为 2024-12-03 08:39:44

不,没有办法“强制”命令提示符执行此操作。

Windows 的设计根本就不像 Unix/Linux。

是否有一个 shell 扩展可以做类似的事情?

我没有听说过,但这应该在超级用户上询问,而不是在这里。

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.

抹茶夏天i‖ 2024-12-03 08:39:44

无法执行随机文件,除非它是实际的可执行二进制文件。 Windows CreateProcess() 函数并不是为此设计的。它唯一可以执行的文件是具有 MZ 魔法的文件或具有 %PATHEXT% 列表中的扩展名的文件。

但是,CMD 本身通过 EXTPROC 子句对自定义解释器的支持有限。限制是解释器在执行时也应该支持并省略该子句。

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.

丑丑阿 2024-12-03 08:39:44

@npocmaka 感谢您的提示!经过一番尝试和错误后,我发现批处理/php 混合的等效内容如下:

<?/** :
@echo off
C:\tools\php81\php.exe -d short_open_tag=On %~f0 %*
exit /b
*/ ?>
<?php
header('Location: example.com/');
print("<body><h1>Hello PHP!<h1></body>");
?> 

@npocmaka Thanks for the hint! After some trial and error I found the equivalent for a batch/php hybrid is as follows:

<?/** :
@echo off
C:\tools\php81\php.exe -d short_open_tag=On %~f0 %*
exit /b
*/ ?>
<?php
header('Location: example.com/');
print("<body><h1>Hello PHP!<h1></body>");
?> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文