2015-12-17 53 views
1

我想使用參數在運行bash腳本節點JS child_process.exec()如何將參數傳遞給節點exec函數中的bash命令?

在文檔是String The command to run, with space-separated arguments,但

child.exec("cat path_to_file1 path_to_file2") 

不起作用。內部更改爲

/bin/sh -c cat path_to_file1 path_to_file2 

失敗。我如何正確運行?在外殼我會修這樣

/bin/sh -c 'cat path_to_file1 path_to_file2' 

(沒寫回調,因爲我只問命令PARAM)

+0

不知道我理解你的問題。 'exec(「cat file1 file2」)'工作正常。 – vinayr

回答

0

你試過類似的東西嗎?

var exec = require('child_process').exec; 
var child; 
child = exec("cat '/route/to/file_a' '/route/to/file_b'", function (error, stdout, stderr) { 
    console.log(stdout); 
    console.log(stderr); 
    if (error !== null) { 
     console.log('exec error: ' + error); 
    } 
}); 

這樣你就可以找到錯誤或得到正確的輸出。 ;)

相關問題