javascript(spidermonkey)如何从js shell运行linux命令? [英] javascript (spidermonkey) how does one run a linux command from js shell?

查看:123
本文介绍了javascript(spidermonkey)如何从js shell运行linux命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难过,感到愚蠢。我已经尝试过每一个我能想到的搜索组合来解决这个问题。这似乎很简单,但是对javascript不熟悉我在搜索示例或演示时没有看到任何有用的东西。
我想使用像python或perl脚本这样的脚本来运行一个简单的linux命令。解释器启动并运行,所以我只是寻找服务器端的js资源,以帮助学习更多有关js的知识。如果我想在浏览器中做这些类型的事情,我发现了许多有用的例子 - 但我不想使用浏览器。
我在网站上找不到这样的查询(虽然我确定我没有问正确的方法)所以如果这确实是一个多余的帖子随时关闭并指出我正确的方向。

I'm stumped and feeling stupid. I've tried every search combination I can think of to figure this out. It seems simple, but being new to javascript I'm not seeing anything helpful in my search for examples or demos. I'm looking to use a script like I would a python or perl script to run a simple linux command. The interpreter is up and running, so I'm just looking for server-side js resources for help in learning more about js. I've found lots of helful examples if I want to do these types of things in a browser - but I don't want to use the browser. I couldn't find a query like this on the site (though I'm sure I'm not asking the right way) so If this is indeed a redundant post feel free to close and point me in the right direction.

推荐答案

你可以使用node.js' exec 来完成。

You can do it with node.js' exec.

# install node.js (debian/ubuntu)
sudo apt-get install nodejs

# run node.js CLI
node

从找到的示例此处

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("ls -la", puts);

这会输出目录列表。

这篇关于javascript(spidermonkey)如何从js shell运行linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆