Linux 的 Javascript 解释器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3422064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript interpreter for Linux
提问by Madalina
Is there a way to run linux commands from javascript that uses a standalone interpreter (something similar with SpiderMonkey, JavaScript shell)?
有没有办法从使用独立解释器(类似于 SpiderMonkey、JavaScript shell)的 javascript 运行 linux 命令?
回答by Alejandro Moreno
You could use NodeJS. It has a child_process module that can run arbitrary commands. E.G. child_process.spawn()
你可以使用 NodeJS。它有一个可以运行任意命令的 child_process 模块。EG child_process.spawn()
When your script is finished you run it like this:
脚本完成后,您可以像这样运行它:
node myscript.js
回答by Franck Freiburger
回答by Dmitry Yudakov
It's possible to define JS functions that will call your C/C++ functions that could use system()call, executing some linux commands.
可以定义 JS 函数来调用你的 C/C++ 函数,这些函数可以使用system()call,执行一些 linux 命令。
Spider Monkey's way: https://developer.mozilla.org/en/JavaScript_C_Engine_Embedder's_Guide#Native_functions
Google V8 is also an option: http://code.google.com/apis/v8/embed.html#accesssors
蜘蛛猴的方式:https: //developer.mozilla.org/en/JavaScript_C_Engine_Embedder's_Guide#Native_functions
Google V8 也是一个选项:http: //code.google.com/apis/v8/embed.html#accesssors
So you would have
所以你会有
system('rpm -i myapp.rpm');
system('rpm -i myapp2.rpm');
or perhaps
也许
install('myapp.rpm');
install('myapp2.rpm');
回答by Aaron Digulla
Rhinooffers a JavaScript interpreter written in Java which can be called from the command line. If you need a browser emulator, try Envjs.
Rhino提供了一个用 Java 编写的 JavaScript 解释器,可以从命令行调用。如果您需要浏览器模拟器,请尝试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
Rhino 无法执行命令,但您可以使用它org.mozilla.javascript.ScriptableObject.defineFunctionProperties()来定义一个新函数,该函数调用一些 Java 代码,您可以在其中创建一个新进程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).
[编辑] 由于 JavaScript 是一种解释型语言,因此您需要一个解释器。要运行解释器,您需要一些其他语言。Linux 没有内置(就像 shell 脚本或类似的一样)。
If you need scripting, use Bash or (for more complex scripts) Python.
如果您需要编写脚本,请使用 Bash 或(对于更复杂的脚本)Python。

