在Windows命令提示中使用shebang / hashbang [英] Use shebang/hashbang in Windows Command Prompt
问题描述
我当前正在使用 serve 脚本通过 Node.js 。在MSYS Shell中或使用 sh
,效果很好,请将node.exe和serve脚本放入我的〜/ bin(在我的PATH中),并且仅键入 serve即可,因为它是 Shebang (#!
)指令,该指令告诉shell使用node运行它。
但是,Windows命令提示符似乎不支持没有* .bat或* .exe扩展名或shebang指令的普通文件。是否可以通过内置的 cmd.exe
强制使用任何注册表项或其他技巧?
我知道我可以编写一个简单的批处理文件以在节点上运行它,但是我想知道它是否可以以内置的方式完成,因此我不必为每个脚本编写脚本,例如
更新:实际上,我在想,是否可以为所有未找到的文件等编写默认处理程序?我可以自动尝试在 sh -c
内执行?
谢谢。
.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
使用控制面板(控制面板->系统->高级系统设置->环境变量对于持久保存
然后,您需要使用命令 FTYPE
和该扩展名注册正确的解释器 ASSOC
:
ASSOC .foo = FooScript
FTYPE FooScript = foorunner.exe%1%*
(上面的例子是从 ftype /?提供的帮助中无耻地获得的。)
ASSOC和FTYPE将直接写入注册表,因此您将需要一个管理帐户来运行它们。
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.
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.
这篇关于在Windows命令提示中使用shebang / hashbang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!