stdout 到文件,stderr 到命令

stdout to a file , stderr to a command

我应该怎么做?

假设我使用 ls / /NotExisting 我希望 / 的结果转到文件 results.txt/NotExisting(假设这是一个错误)到转到命令 cksum

我可以像 ls / /NotExisting > results.txt

那样单独做

ls / /NotExisting 2> >(cksum) 这给了我一些类似于我想要实现的东西,但还没有完全实现。那么如何将这两行连接在一起呢?

ls / /NotExisting 2>&1 >results.txt | cksum

你知道如果你想将 stdout 和 stderr 都发送到一个文件,你需要写
somecommand >file 2>&1 而另一个命令不起作用?那么它 "doesn't work" 的方式正是您想要的这个例子。

First,此命令将 stderr 重定向到 stdout 当前所在的位置(这将是 cksum 的管道输入),然后 它将 stdout 重定向到 results.txt(这不会影响 stderr)。