Supposedly jjs
是 Java 8 版本的 nashorn,ready for shell-scripting 。但是,当我编写具有执行权限的文件时:
#!/usr/bin/jjs
arguments.forEach(function(v,i,a){print(v);});
运行它会产生一些不太理想的 shell 脚本行为:
$./file.js
$./file.js one two
java.io.IOException: one is not a file
$./file.js -- one two
one
two
$./file.js file.js -- one two # what were they thinking
one
two
one
two
$
所以,我不希望我编写的这个脚本允许在运行后对文件进行任意解释,也许我应该使用 --
在 shebang 中,例如:
#!/usr/bin/jjs --
arguments.forEach(function(v,i,a){print(v);});
但这变得不太有用:
$./file.js
jjs>^D
$./file.js one two
jjs>^D
$./file.js -- one two
jjs>
是的,我认为不应该发生这种情况。
我应该如何才能使用直接传递的参数执行脚本而不是由 jjs 本身解释,以便我可以获得如下行为:
$./file.js one two
one
two
$
最佳答案
该示例在 JDK 9 版本的 Nashorn 中按预期工作。我将负责向后移植所需的补丁,以便它们可以出现在即将发布的 8u 版本之一中。
更新:所需的更改已向后移植到 8u 流,但尚不清楚何时发布。 JDK 9 的早期访问版本可从 https://jdk9.java.net/download/ 获取。具有 shebang 功能,以及其他扩展,例如内置 $EXEC 的管道支持。
关于java - 编写仅接受参数的 `jjs` shebanged 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37096520/