为什么不能影响此管道输出的程序?

时间:2015-05-23 14:44:16

标签: bash pipe

我将命令的输出传递给sort,但我回来的结果并没有排序:

$ plushu logs foo | sort
2015-05-23T11:07:18.294569666Z Unhandled rejection RqlDriverError: First argument to `run` must be an open connection.
2015-05-23T11:07:18.294627441Z     at new RqlDriverError (/app/node_modules/endex/node_modules/rethinkdb/errors.js:14:13)
2015-05-23T11:07:18.294632871Z     at MakeArray.TermBase.run (/app/node_modules/endex/node_modules/rethinkdb/ast.js:129:29)
2015-05-23T11:07:18.294636744Z     at /app/node_modules/endex/index.js:131:24
2015-05-23T11:07:18.294640994Z     at tryCatcher (/app/node_modules/endex/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294644642Z     at Promise._resolveFromResolver (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:427:31)
2015-05-23T11:07:18.294648772Z     at new Promise (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:53:37)
2015-05-23T11:07:18.294652389Z     at Object.endexRun [as run] (/app/node_modules/endex/index.js:130:17)
2015-05-23T11:07:18.294656144Z     at /app/index.js:16:8
2015-05-23T11:07:18.294659485Z     at tryCatcher (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294662897Z     at Promise._settlePromiseFromHandler (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:454:31)
2015-05-23T11:07:18.294666862Z     at Promise._settlePromiseAt (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:530:18)
2015-05-23T11:07:18.294670210Z     at Promise._settlePromises (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:646:14)
2015-05-23T11:07:18.294673750Z     at Async._drainQueue (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:177:16)
2015-05-23T11:07:18.294677000Z     at Async._drainQueues (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:187:10)
2015-05-23T11:07:18.294691659Z     at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:15:14)
2015-05-23T11:07:18.294698794Z     at processImmediate [as _immediateCallback] (timers.js:358:17)
2015-05-23T11:03:47.067095017Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:03:47.067302784Z Recommending WEB_CONCURRENCY=1
2015-05-23T11:07:17.513011019Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:07:17.513270538Z Recommending WEB_CONCURRENCY=1

运行plushu logs foo | grep WEB时,我得到的结果相同。

这个输出有什么问题导致sort无法正常工作(结果不按顺序),而grep正在打印每一行,未修改,没有过滤?

1 个答案:

答案 0 :(得分:1)

确保您的管道输出到STDOUT。 STDERR(正在打印错误消息)不会通过管道,并将不加修改地输出到您的控制台。 (在这种情况下,STDERR输出即将到来from the logs command in Docker。)

您可以通过redirecting file descriptor 2检查输出是否来自STDERR:

$ plushu logs foo 2>/tmp/test-errors.log | sort
2015-05-23T11:03:47.067095017Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:03:47.067302784Z Recommending WEB_CONCURRENCY=1
2015-05-23T11:07:17.513011019Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:07:17.513270538Z Recommending WEB_CONCURRENCY=1
$ cat /tmp/test-errors.log
2015-05-23T11:07:18.294569666Z Unhandled rejection RqlDriverError: First argument to `run` must be an open connection.
2015-05-23T11:07:18.294627441Z     at new RqlDriverError (/app/node_modules/endex/node_modules/rethinkdb/errors.js:14:13)
2015-05-23T11:07:18.294632871Z     at MakeArray.TermBase.run (/app/node_modules/endex/node_modules/rethinkdb/ast.js:129:29)
2015-05-23T11:07:18.294636744Z     at /app/node_modules/endex/index.js:131:24
2015-05-23T11:07:18.294640994Z     at tryCatcher (/app/node_modules/endex/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294644642Z     at Promise._resolveFromResolver (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:427:31)
2015-05-23T11:07:18.294648772Z     at new Promise (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:53:37)
2015-05-23T11:07:18.294652389Z     at Object.endexRun [as run] (/app/node_modules/endex/index.js:130:17)
2015-05-23T11:07:18.294656144Z     at /app/index.js:16:8
2015-05-23T11:07:18.294659485Z     at tryCatcher (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294662897Z     at Promise._settlePromiseFromHandler (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:454:31)
2015-05-23T11:07:18.294666862Z     at Promise._settlePromiseAt (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:530:18)
2015-05-23T11:07:18.294670210Z     at Promise._settlePromises (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:646:14)
2015-05-23T11:07:18.294673750Z     at Async._drainQueue (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:177:16)
2015-05-23T11:07:18.294677000Z     at Async._drainQueues (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:187:10)
2015-05-23T11:07:18.294691659Z     at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:15:14)
2015-05-23T11:07:18.294698794Z     at processImmediate [as _immediateCallback] (timers.js:358:17)

(原始问题中给出的示例grep输出显示未修改,因为 通过STDOUT的唯一4行都匹配模式 - 所有其他行,而他们不&# 39; t匹配模式,也不通过grep。)

要将STDERR输出发送到管道,您可以将STDOUT(&1)的文件描述符复制到STDERR(&2),以获取要生成要通过的STDERR输出的命令。管:

$ plushu logs foo 2>&1 | sort -sk1,1
2015-05-23T11:03:47.067095017Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:03:47.067302784Z Recommending WEB_CONCURRENCY=1
2015-05-23T11:07:17.513011019Z Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-05-23T11:07:17.513270538Z Recommending WEB_CONCURRENCY=1
2015-05-23T11:07:18.294569666Z Unhandled rejection RqlDriverError: First argument to `run` must be an open connection.
2015-05-23T11:07:18.294627441Z     at new RqlDriverError (/app/node_modules/endex/node_modules/rethinkdb/errors.js:14:13)
2015-05-23T11:07:18.294632871Z     at MakeArray.TermBase.run (/app/node_modules/endex/node_modules/rethinkdb/ast.js:129:29)
2015-05-23T11:07:18.294636744Z     at /app/node_modules/endex/index.js:131:24
2015-05-23T11:07:18.294640994Z     at tryCatcher (/app/node_modules/endex/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294644642Z     at Promise._resolveFromResolver (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:427:31)
2015-05-23T11:07:18.294648772Z     at new Promise (/app/node_modules/endex/node_modules/bluebird/js/main/promise.js:53:37)
2015-05-23T11:07:18.294652389Z     at Object.endexRun [as run] (/app/node_modules/endex/index.js:130:17)
2015-05-23T11:07:18.294656144Z     at /app/index.js:16:8
2015-05-23T11:07:18.294659485Z     at tryCatcher (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/util.js:24:31)
2015-05-23T11:07:18.294662897Z     at Promise._settlePromiseFromHandler (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:454:31)
2015-05-23T11:07:18.294666862Z     at Promise._settlePromiseAt (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:530:18)
2015-05-23T11:07:18.294670210Z     at Promise._settlePromises (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:646:14)
2015-05-23T11:07:18.294673750Z     at Async._drainQueue (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:177:16)
2015-05-23T11:07:18.294677000Z     at Async._drainQueues (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:187:10)
2015-05-23T11:07:18.294691659Z     at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:15:14)
2015-05-23T11:07:18.294698794Z     at processImmediate [as _immediateCallback] (timers.js:358:17)

this answer to a separate question中用于排序时间戳的-sk1,1参数的说明。)