如何从Linux shell执行javascript程序?

时间:2011-03-18 07:03:05

标签: javascript linux shell kernel rhino

我想用Rhino shell执行我的javascript程序而不用第一行#!/ bin / sh。 '#'不是javascript中的评论字符。我也不想要有.js扩展名。

这可能吗?我记得在很长一段时间内阅读了一些让java程序在没有显式解释器的情况下运行的方法。然而谷歌尚未出现。

2 个答案:

答案 0 :(得分:2)

您需要使用内核模块binfmt_misc。我没有安装JS解释器,但我猜犀牛的魔法就像这样:

# If it's not included in your kernel
sudo modprobe binfmt_misc
sudo mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc

# Register the .js extension as being run by Rhino
echo ':JSScript:E::js::/path/to/rhino:' > /proc/sys/fs/binfmt_misc/register

这假设您的脚本很乐意像以下一样运行:

 /path/to/rhino /path/to/your/script

如果你需要将任何参数传递给Rhino,那么你需要设置一个包装器。 binfmt_misc的完整文档为here

答案 1 :(得分:0)

是的,只需致电rhino /path/to/your/js/file

如果您的路径中没有程序rhino,请将其替换为java org.mozilla.javascript.tools.shell.Main