得到shell命令的node.js的输出 [英] Get the output of a shell command in node.js
本文介绍了得到shell命令的node.js的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个Node.js的,我想找到一种方法来获得一个Unix终端命令的输出。有没有办法做到这一点?
函数getCommandOutput(CommandString中){
//现在我怎样才能实现这个功能呢?
// getCommandOutput(LS)应该打印shell命令的输出端的ls
}
解决方案
这就是我做的一个项目我现在的工作方式。
VAR EXEC =要求('child_process')EXEC。
执行功能(命令,回调){
EXEC(指挥,功能(错误,标准输出,标准错误){回调(标准输出);});
};
例子:获取git的用户
module.exports.getGitUser =函数(回调){
执行(混帐配置--global user.name功能(名称){
执行(混帐配置--global user.email功能(电子邮件){
回调({名称:name.replace(\\ n,),电子邮件:email.replace(\\ n,)});
});
});
};
In a node.js, I'd like to find a way to obtain the output of a Unix terminal command. Is there any way to do this?
function getCommandOutput(commandString){
//now how can I implement this function?
//getCommandOutput("ls") should print the terminal output of the shell command "ls"
}
解决方案
Thats the way I do it in a project I am working now.
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
Example: Retrieving git user
module.exports.getGitUser = function(callback){
execute("git config --global user.name", function(name){
execute("git config --global user.email", function(email){
callback({ name: name.replace("\n", ""), email: email.replace("\n", "") });
});
});
};
这篇关于得到shell命令的node.js的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文