linux - 如何将输出重定向到文件和标准输出

在 bash 中,调用 foo 会在标准输出上显示该命令的任何输出。

调用 foo > output 会将来自该命令的任何输出重定向到指定的文件(在本例中为“输出”)。

有没有办法将输出重定向到一个文件让它显示在标准输出上?

最佳答案

你想要的命令被命名为 tee :

foo | tee output.file

例如,如果你只关心标准输出:

ls -a | tee output.file

如果您想包含标准错误,请执行以下操作:

program [arguments...] 2>&1 | tee outfile

2>&1 将 channel 2(stderr/标准错误)重定向到 channel 1(stdout/标准输出),这样两者都写为 stdout。从 tee 命令开始,它也被定向到给定的输出文件。

此外,如果您想追加到日志文件,请使用 tee -a 作为:

program [arguments...] 2>&1 | tee -a outfile

https://stackoverflow.com/questions/418896/

相关文章:

python - 如何正确确定当前脚本目录?

python - 如何判断 tensorflow 是否从 python shell 内部使用 gpu

python - 连接两个一维 NumPy 数组

python - Pandas 'count(distinct)' 等效

linux - "POSIX"是什么意思?

linux - 如何使用 sudo 将输出重定向到我无权写入的位置?

python - 下标序列时Python中的::(双冒号)是什么?

python - 创建单独变量字典的更简单方法?

linux - 杀死分离的 screen session

linux - 在 Bash 脚本中通过管道传入/传出剪贴板