如何从 Linux shell 执行 javascript 程序?
我想使用 Rhino shell 执行我的 javascript 程序,而不使用第一行 #!/bin /sh。 '#' 不是 JavaScript 中的注释字符。我也不想有 .js 扩展名。
这可能吗?我记得读了很长一段时间关于使 java 程序在没有显式解释器的情况下运行的方法的内容。但谷歌尚未表态。
I would like to execute my javascript programs with the Rhino shell without making the first line #!/bin/sh. '#' isn't a comment character in javascript. I also don't want to have to have a .js extension.
Is this possible? I remember reading something a long while about a method of making java programs to run without an explicit interpreter. Google has not been forthcoming, however.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您需要使用内核模块
binfmt_misc
。我没有安装 JS 解释器,但我想 Rhino 的魔力是这样的:这假设你的脚本很乐意像这样运行:
如果你需要将任何参数传递给 Rhino,那么你需要设置包装器。
binfmt_misc
的完整文档位于此处。You'll want to use the kernel-module
binfmt_misc
for this. I don't have a JS interpreter installed, but I guess the magic would be something like this for Rhino:This assumes that your scripts are happy to be run like:
If you need to pass any arguments to Rhino, then you'll need to set up a wrapper. Full documentation for
binfmt_misc
is here.是的,只需调用
rhino /path/to/your/js/file
如果您的路径中没有程序
rhino
,请将其替换为java org.mozilla.javascript.tools.shell.Main
Yes, simply call
rhino /path/to/your/js/file
In case you don't have a program
rhino
in your path, replace it withjava org.mozilla.javascript.tools.shell.Main