node.js同步执行系统命令 [英] node.js execute system command synchronously

查看:190
本文介绍了node.js同步执行系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 node.js 功能

  result = execSync('node -v' ); 

同步执行给定的命令行并返回所有stdout通过该命令文本。


ps。同步错误。我知道。只供个人使用。


UPDATE



现在我们有mgutz的解决方案,它给我们退出代码,但不是stdout!仍在等待更准确的答案。



UPDATE



mgutz 更新了他的答案,解决方案在这里:)

同样, dgo.a 提到,有独立模块exec-sync



UPDATE 2014-07-30



ShellJS lib到达。现在考虑这是最好的选择。






UPDATE 2015-02-10 p>

到最后! NodeJS 0.12本身支持 execSync

官方文档

解决方案

NodeJS 0.12和IO.js 1.10现在支持 execSync


child_process.execSync(command [,options])#


现在可以直接执行:

  const execSync = require('child_process')。execSync; 
code = execSync('node -v');

它会做你期望的。 (默认为将io结果传递给父进程)。请注意,您现在还可以 spawnSync


I need in node.js function

result = execSync('node -v');

that will synchronously execute the given command line and return all stdout'ed by that command text.

ps. Sync is wrong. I know. Just for personal use.

UPDATE

Now we have mgutz's solution which gives us exit code, but not stdout! Still waiting for a more precise answer.

UPDATE

mgutz updated his answer and the solution is here :)
Also, as dgo.a mentioned, there is stand-alone module exec-sync

UPDATE 2014-07-30

ShellJS lib arrived. Consider this is the best choice for now.


UPDATE 2015-02-10

AT LAST! NodeJS 0.12 supports execSync natively.
See official docs

解决方案

NodeJS 0.12 and IO.js 1.10 now support execSync:

child_process.execSync(command[, options])#

You can now directly do this:

const execSync = require('child_process').execSync;
code = execSync('node -v');

and it'll do what you expect. (Defaults to pipe the io results to the parent process). Note that you can also spawnSync now.

这篇关于node.js同步执行系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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