我想重定向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
放在一个不直观的地方在管道情况!;)
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/ |
--- +--------------+ --- +--------------+