views:

74

answers:

3

Is there a way to run linux commands from javascript that uses a standalone interpreter (something similar with SpiderMonkey, JavaScript shell)?

A: 

Rhino offers a JavaScript interpreter written in Java which can be called from the command line. If you need a browser emulator, try Envjs.

Rhino can't execute commands but you can use org.mozilla.javascript.ScriptableObject.defineFunctionProperties() to define a new function which calls some Java code in which you can create a new process using ProcessBuilder

[EDIT] Since JavaScript is an interpreted language, you need an interpreter. For the interpreter to run, you need some other language. Linux doesn't come with one built-in (like it does for shell scripts or similar).

If you need scripting, use Bash or (for more complex scripts) Python.

Aaron Digulla
I don't need a browser emulator. All I want to do is run a javascript file (using an interpretor) that runs some linux commands. Is it possible to do this? And also I would prefer not to depend on Java, because my javascript file should make some installations on my linux machine. I don't know if my questions is clear, but I was suggested to use javascript for doing this, and I don't realy know how.
Madalina
Since JavaScript is an interpreted language, you need an interpreter. For the interpreter to run, you need some other language.
Aaron Digulla
+1  A: 

It's possible to define JS functions that will call your C/C++ functions that could use system() call, executing some linux commands.

So you would have

system('rpm -i myapp.rpm');
system('rpm -i myapp2.rpm');

or perhaps

install('myapp.rpm');
install('myapp2.rpm');
Dmitry Yudakov
Yes, I need to run something like system('rpm -i myapp.rpm'), but directly from Javascript, because I can't depend on other languages, like C++ or Java.
Madalina
A: 

You could use NodeJS. It has a child_process module that can run arbitrary commands. E.G. child_process.spawn()

When your script is finished you run it like this:

node myscript.js
Alejandro Moreno