我有一個shell命令,在終端運行正常但由節點的child_process
執行錯誤。節點的child_process中奇怪的管道行爲?
這裏是作爲終端使用(file.json是一個JSON文件)的命令:
cat /tmp/file.json | jq
在這裏從child_process
運行相同的指令:
var cp = require("child_process");
var command = "cat /tmp/gen_json | jq";
cp.exec(command, function(err, stdout, stderr) {
stderr ? console.log(stderr) : console.log(stdout);
});
哪個生產:
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]
jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
...
This是剛剛運行時顯示的默認消息jq
。就好像我只是跑了jq
沒有前面的管道。
不涉及到管道的行爲,但你並不需要在這裏'cat' (如果這是你正在運行的真正命令)。試試'jq'..filter ..'/ tmp/file.json'。 – randomir
@randomir在我的情況下,我確實需要'cat',因爲如果我運行'jq/tmp/file.json',它會打斷'unexpected'/''。當'cat'在'jq'之前解析文件時,我沒有看到這個錯誤。 (用pastebin更新問題到json) –
看起來像'jq'需要強制過濾器,請嘗試:'cat file | jq'。''。這也解決了你的其他問題:'jq'。' file'。 – randomir