2015-02-10 123 views
1

IAM的嘗試使用的node.js產卵執行一個終端命令

執行終端指令爲正在使用的代碼

console.log(args) 

var child = spawn("hark", args, {cwd: workDir}); 
     child.stdout.on('data', function(data) { 

      console.log(data.toString())   
     }); 

     child.stderr.on('data', function(data) { 
      console.log('stdout: ' + data); 

     }); 

     child.on('close', function(code) { 
      console.log('closing code: ' + code); 

     }); 

But it treated greater than>as string「>」和 獲得輸出作爲

tshark: Invalid capture filter "> g.xml" 

    That string isn't a valid capture filter (syntax error). 
    See the User's Guide for a description of the capture filter syntax. 

如何使用>沒有串

+0

'> ggggg.xml'是用於將stdout重定向到文件的shell語法。你應該使用spawn http://nodejs.org/api/child_process.html#child_process_options_stdio的正確選項或使用'child_process.exec'來調用shell。 – 2015-02-10 07:51:30

回答

2

您可以使用文件流將產生的hark的所有輸出設置爲g.xml
例子:

// don't need ">" in args 
var args = [' 02:00:00:00' ,'-s','pdml'], 
    logStream = fs.createWriteStream('./xml'); 

var spawn = require('child_process').spawn, 
    child = spawn('tshark', args); 

child.stdout.pipe(logStream); 
child.stderr.pipe(logStream); 

child.on('close', function (code) { 
    console.log('child process exited with code ' + code); 
}); 

因爲child這裏是一個流,你可以管到你的記錄文件流。

+0

是的一個更多的事.. ..編碼工作..但它沒有輸出有效的XML文件....一些屬性丟失 – Psl 2015-02-13 09:35:45

+0

嘗試玩選項:http://nodejs.org/api/fs.html #fs_fs_createreadstream_path_options – 2015-02-13 09:55:53

+0

它是創建讀取流...你可以更新答案 – Psl 2015-02-13 10:07:26