使用node.js执行Shell程序并传递参数 [英] Execute an shell program with node.js and pass params

查看:688
本文介绍了使用node.js执行Shell程序并传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个需要使用Node.js进行任何参数设置的Shell程序

I would like to execute an shell program who require any params with Node.js

我该怎么做?

推荐答案

来自: http://www.dzone.com/snippets/execute-unix-command-nodejs

要执行shell命令:

To execute shell commands:

var sys = require('sys')
var exec = require('child_process').exec;

exec('command', function (error, stdout, stderr) {});

来自:使用node.js(childProcess)运行shell脚本

要在主文件夹中运行带有参数'foo'的程序bar.sh:

To run a program bar.sh in your home folder with the argument 'foo':

var foo = 'foo';

exec('~/bar.sh ' + foo,
  function (error, stdout, stderr) {
    if (error !== null) {
      console.log(error);
    } else {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    }
});

这篇关于使用node.js执行Shell程序并传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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