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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:07:36  来源:igfitidea点击:

Javascript interpreter for Linux

javascriptlinuxshell

提问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

jslibsis a standalone JavaScript interpreter that runs on Linux32/64 and Windows.
You can easily run linux commands through the libraries provided by jslibs.

jslibs是一个独立的 JavaScript 解释器,可以在 Linux32/64 和 Windows 上运行。
您可以通过jslibs提供的库轻松运行 linux 命令。

回答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 命令。

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