java - 编写仅接受参数的 `jjs` shebanged 脚本

标签 java shell nashorn jjs

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/

相关文章:

java - Lodash 模板引擎 Nashorn

java - Nashorn 类型错误 : Cannot call undefined in <eval>

java - 如何使字体文本可点击?

algorithm - KornShell 从具有 n 个对象的集合中生成 k 个对象的组合数

linux - 如何在领域加入事件目录命令中提供密码作为参数

python - 激活 venv 并从 shell 脚本运行 python 脚本

java - Box2d 不会使用 LibsGDX 和 Gradle 进行加载

java - 在java代码中向密码查询插入字符串参数时出错

java - 如何在 java 中将 json 对象转换为 HTML 格式?

java - Nashorn 中新的 Java 字符串数组