2015-06-18 77 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

相關問題