2015-06-18 75 views

回答

2

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

执行shell命令:

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

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

来源:Run shell script with node.js (childProcess)

为了与参数 '富' 运行在您的个人文件夹中的程序bar.sh:

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); 
    } 
}); 
+0

非常感谢。 现在,我怎样才能读取这个参数在我的.sh请 – Matthieu

+1

参数可以在shell脚本中用$ X来访问---其中X是参数传递的顺序。 如果你的脚本bar.sh包含 'echo第一个参数:$ 1,第二PARAM:运行'sh bar.sh富baz' 更多的时候 $ 2' 这将打印 “巴兹第一个参数:FOO,第二个参数” info [here](http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html) – krekle

+0

完美谢谢! – Matthieu

相关问题