Javascript as command line callable scripting lang

2019-02-18 18:20发布

I'm looking for a way to write some simple scripts in javascript, like I would in ruby. For example, I might write a script like:

var str = "Hello World";
console.log(str);

And i want to able to call it from my command line like this:

js hello_world.js

Is there some sort of Javascript runtime, that includes a standard library that would allow me to do this style of script development? I know there is node.js, but that is specific to a web server, right?

6条回答
放我归山
2楼-- · 2019-02-18 18:42

what about mozilla's spidermonkey ?

apt-cache search javascript interpreter
spidermonkey-bin - standalone JavaScript/ECMAScript (ECMA-262) interpreter
libjenkins-htmlunit-core-js-java - Jenkins branch of the HtmlUnit Core JS Interpreter
gnome-js-common - Common modules for GNOME JavaScript interpreters
查看更多
萌系小妹纸
3楼-- · 2019-02-18 18:45

With Phantom JS you can easily run a javascript from command line and it naively supports DOM, CSS Selector and JSON.

查看更多
We Are One
5楼-- · 2019-02-18 18:52

Why do you want to do this in javascript? Why not use a language that is suitable for this type of command line environment such as Perl? Javascript's main purpose is to faciltate client-side browser control.

查看更多
干净又极端
6楼-- · 2019-02-18 19:02

As mentioned in this previous question, you might want to check out Rhino, an implementation of Javascript written in Java. Specifically, the page on the JavaScript shell might be of interest.

Hope this helps.

查看更多
贪生不怕死
7楼-- · 2019-02-18 19:04

Node.js has emerged as the clear winner here, at least for me.

http://nodejs.org/

查看更多
登录 后发表回答