如何重定向shell脚本的所有输出的副本



我想重定向shell脚本的所有输出的副本,脚本中有一些挂载命令和一些回显。

如果我使用>> 2>&1,我看不到命令行的输出。
如果我使用| tee -a the_log_file 2>&1,我可以在命令行中获得所有输出,但在_log_file中,没有挂载错误输出,如mount.nfs: /mnt/folder is busy or already mounted,我也想在the_log_file

我该如何解决这个问题?

你需要使用
command 2>&1 | tee -a the_log_file
而不是
command | tee -a the_log_file 2>&1

(如果你问我,这是非常不直观的,你不需要把2>&1放在一个不直观的地方在管道情况!;)

有关详细信息,请查看Bash Hackers Wiki中的插图重定向教程复制文件描述符2>&1一节中的示例显示了我们的情况:
  ls /tmp/ doesnotexist 2>&1     |                   less
 ---       +--------------+              ---       +--------------+
( 0 ) ---->| /dev/pts/5   |     ------> ( 0 ) ---->|from the pipe |
 ---       +--------------+    /   --->  ---       +--------------+
                              /   /
 ---       +--------------+  /   /       ---       +--------------+
( 1 ) ---->| to the pipe  | /   /       ( 1 ) ---->|  /dev/pts    |
 ---       +--------------+    /         ---       +--------------+
                              /
 ---       +--------------+  /           ---       +--------------+
( 2 ) ---->|  to the pipe | /           ( 2 ) ---->| /dev/pts/    |
 ---       +--------------+              ---       +--------------+

相关内容

最新更新