J-u*_*ior 31 shell shell-script javascript
最近我一直在和 JS 一起工作,我对这门语言非常热情。我知道有node.js用于在服务器端运行 JS,但是是否有使用 JS 作为脚本语言的 shell?如果存在这样的东西,它的可用性和稳定性如何?
sr_*_*sr_ 18
这看起来对你来说是可取的吗?
// Replace macros in each .js file
cd('lib');
ls('*.js').forEach(function(file) {
sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
});
cd('..');
Run Code Online (Sandbox Code Playgroud)
如果是这样,ShellJS可能会很有趣,它是
在 Node.js API 之上的 Unix shell 命令的可移植(包括 Windows)实现。
不过,我不确定这是否可以用作功能齐全的登录 shell。(也许有一些包装?)
您可能会争辩说它不是真正的外壳,但您知道TermKit吗?它是由 Node.js + Webkit 组成的,你可以用 JS 来扩展它(我猜);shell 语言仍然是 Bash(-ish)。
Mozilla INC 提供了一个javascript
shell:参见Introduction_to_the_JavaScript_shell
系统shell中的javascript shell示例:
$ js
js> str = "welcome on *nix side"
"welcome on *nix side"
js> str.indexOf("nix");
12
js>
Run Code Online (Sandbox Code Playgroud)
由于我将其用于测试目的,因此未检测到任何问题。
如果您使用的是 Ubuntu 或任何其他基于 debian 的系统,您可以安装 rhino(来自 Mozilla.org)。
sudo apt-get install rhino
Run Code Online (Sandbox Code Playgroud)
它通过替代方案提供 js:
=== /usr/bin/js is a symlink... following it
lrwxrwxrwx 1 root root 20 Nov 21 08:54 /usr/bin/js -> /etc/alternatives/js
=== /etc/alternatives/js is a symlink... following it
lrwxrwxrwx 1 root root 14 Nov 21 08:54 /etc/alternatives/js -> /usr/bin/rhino
=== /usr/bin/rhino is owned by package: rhino ===
Run Code Online (Sandbox Code Playgroud)
所以调用rhino或js都会给你一个 JavaScript shell。
编辑(2014-06-30):
rhino可以很好地快速测试文件中的一些 javascript 代码,但它不是交互式 shell,因此它不支持 GNU readline 样式的编辑。对于交互式工作,您可能更喜欢nodejs
: 在 Ubuntu/debian 上sudo apt-get install nodejs
。这应该提供一个更具交互性的js
shell(使用命令调用js
),您可以在其中使用向上/向下箭头键编辑行和调用历史记录。有关更长的选项列表,请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells