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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6818031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:34:34  来源:igfitidea点击:

Use shebang/hashbang in Windows Command Prompt

windowsunixshellcommand-promptshebang

提问by Adam M-W

I'm currently using the servescript to serve up directories with Node.json 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.

我目前正在使用服务脚本在 Windows 7 上使用Node.js提供目录。它在 MSYS shell 或使用中运行良好sh,因为我已将 node.exe 和服务脚本放在我的 ~/bin (这是在我的 PATH 上),并且只输入“serve”是有效的,因为它是Shebang( #!) 指令,它告诉 shell 用node.js运行它。

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?

但是,Windows 命令提示符似乎不支持没有 *.bat 或 *.exe 扩展名的普通文件,也不支持 shebang 指令。是否有任何注册表项或其他黑客可以强制这种行为脱离内置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?

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

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?

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

Thanks.

谢谢。

回答by a_horse_with_no_name

Yes, this is possible using the PATHEXTenvironment variable. Which is e.g. also used to register .vbsor .wshscripts to be run "directly".

是的,这可以使用PATHEXT环境变量。例如,这也用于注册.vbs.wsh“直接”运行的脚本。

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

首先,您需要扩展PATHEXT变量以包含该服务脚本的扩展名(在下面我假设扩展名是 .foo,因为我不知道 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.

使用控制面板(控制面板 -> 系统 -> 高级系统设置 -> 环境变量是保持 PATHEXT 变量值所必需的。

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

然后,您需要使用以下命令FTYPE和该扩展名注册正确的“解释器” ASSOC

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

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

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

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

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

回答by Doug

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

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

回答by user541686

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

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

Windows simply wasn't designedlike Unix/Linux.

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

Is there a shell extensionthat does something similar?

有没有类似的外壳扩展

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

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

回答by Doug

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.

这是一种强制 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%

回答by AnrDaemon

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 MZmagic or with extensions from %PATHEXT%list.

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

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

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

回答by npocmaka

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 .bator .cmdextension):

命令提示符不支持 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));

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

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