views:

175

answers:

6

Is there a JavaScript interpretor available in Bash (or for that matter any other shell) just like there is for Perl and Python. I have written some JavaScript code as part of web programming and was wondering if its used as a shell scripting language as well?

Note : Please feel free to edit this question, if it feels subjective.

+4  A: 

There's a command line version of the Rhino JavaScript interpreter.

Mark Byers
This looks quite useful. Is there any interpreter which does not require Java ?
vivekian2
A: 

It is called Rhino.

jldupont
+1  A: 

Windows has built-in javascript scripting. Double-click on any .js file to execute it.

Gabe Moothart
+3  A: 

Most JavaScript engines have a standalone interpreter available:

  • Spidermonkey (Firefox's interpreter) I couldn't find a direct for the shell, but I'm pretty sure it's part of the normal download.
  • Rhino (Mozilla's Java-based interpreter)
  • V8 (Google Chrome's interpreter)

I've found Rhino to be the most useful, because you can access any Java classes. The Spidermonkey and V8 shells are pretty basic. You could also try the Narwhal project, which has a CommonJS-compatible library for multiple engines. Rhino is their primary engine and has the best support though. I haven't tried it on any other engines.

Matthew Crumley
I use spidermonkey frequently
glenn jackman
@glenn: Assuming you mean with narwhal, do you know how it compares to the Rhino implementation, as far as completeness?
Matthew Crumley
@Matthew, no, I compiled Spidermonkey as a standalone interpreter. Never used narwhal.
glenn jackman
+2  A: 

JavaScript is not useful as a general purpose scripting language. JavaScript proper provides no access to the local filesystem. Depending on the implementation of the interpreter, you can do extra stuff.

SpiderMonkey extends JavaScript with print() and readline() functions, but that only gets you stdin and stdout. You can't open a text file and read it. There's nothing analogous to Perl's system() command.

JScript is build on top of Windows Scripting Host, so you can do just about anything by creating ActiveX objects (like you'd do with VBScript).

glenn jackman
A: 

you can build your own v8 engine, it doesn't require Java. Here is the documentation - http://code.google.com/apis/v8/build.html

Spartan