我知道如何使用tee
将aaa.sh
的输出( STDOUT
) aaa.sh
bbb.out
,同时仍将其显示在终端中: html
./aaa.sh | tee bbb.out
如今如何将STDERR
写入一个名为ccc.out
的文件,同时仍然显示它? shell
换句话说,您但愿将stdout传递到一个过滤器( tee bbb.out
),将stderr tee bbb.out
给另外一个过滤器( tee ccc.out
)。 除了将stdout传送到另外一个命令外,没有其余标准方法能够经过管道传递,可是您能够经过处理文件描述符来解决。 bash
{ { ./aaa.sh | tee bbb.out; } 2>&1 1>&3 | tee ccc.out; } 3>&1 1>&2
另请参见如何grep标准错误流(stderr)? 以及什么时候使用附加的文件描述符? this
在bash(以及ksh和zsh)中,但在其余POSIX shell(例如破折号)中却没有,能够使用进程替换 : spa
./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out)
请注意,在bash中,即便仍执行tee
命令(ksh和zsh确实等待子./aaa.sh
,该命令也会在./aaa.sh
完成后当即返回。 若是您执行./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out
这多是一个问题./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out
./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out
。 在这种状况下,请改用文件描述符杂耍或ksh / zsh。 日志
这对于经过Google找到此信息的人可能有用。 只需取消注释您要尝试的示例。 固然,能够随意重命名输出文件。 code
#!/bin/bash STATUSFILE=x.out LOGFILE=x.log ### All output to screen ### Do nothing, this is the default ### All Output to one file, nothing to the screen #exec > ${LOGFILE} 2>&1 ### All output to one file and all output to the screen #exec > >(tee ${LOGFILE}) 2>&1 ### All output to one file, STDOUT to the screen #exec > >(tee -a ${LOGFILE}) 2> >(tee -a ${LOGFILE} >/dev/null) ### All output to one file, STDERR to the screen ### Note you need both of these lines for this to work #exec 3>&1 #exec > >(tee -a ${LOGFILE} >/dev/null) 2> >(tee -a ${LOGFILE} >&3) ### STDOUT to STATUSFILE, stderr to LOGFILE, nothing to the screen #exec > ${STATUSFILE} 2>${LOGFILE} ### STDOUT to STATUSFILE, stderr to LOGFILE and all output to the screen #exec > >(tee ${STATUSFILE}) 2> >(tee ${LOGFILE} >&2) ### STDOUT to STATUSFILE and screen, STDERR to LOGFILE #exec > >(tee ${STATUSFILE}) 2>${LOGFILE} ### STDOUT to STATUSFILE, STDERR to LOGFILE and screen #exec > ${STATUSFILE} 2> >(tee ${LOGFILE} >&2) echo "This is a test" ls -l sdgshgswogswghthb_this_file_will_not_exist_so_we_get_output_to_stderr_aronkjegralhfaff ls -l ${0}
以我为例,脚本在将stdout和stderr都重定向到文件时正在运行命令,例如: orm
cmd > log 2>&1
我须要对其进行更新,以便在出现故障时根据错误消息采起一些措施。 我固然能够删除dup 2>&1
并从脚本中捕获stderr,可是错误消息不会进入日志文件以供参考。 虽然@lhunath的可接受答案应该是相同的,但它会将stdout
和stderr
重定向到不一样的文件,这不是我想要的,但它帮助我提出了所需的确切解决方案: htm
(cmd 2> >(tee /dev/stderr)) > log
经过以上操做,日志将同时包含stdout
和stderr
的副本,而且我能够从脚本中捕获stderr
而没必要担忧stdout
。 进程
如下内容适用于没法进行进程替换的KornShell(ksh),
# create a combined(stdin and stdout) collector exec 3 <> combined.log # stream stderr instead of stdout to tee, while draining all stdout to the collector ./aaa.sh 2>&1 1>&3 | tee -a stderr.log 1>&3 # cleanup collector exec 3>&-
这里的实际伎俩,是序列2>&1 1>&3
这在咱们的状况下,重定向stderr
到stdout
和所述重定向stdout
到描述符3
。 此时, stderr
和stdout
还没有合并。
实际上,将stderr
(做为stdin
)传递到tee
,并在其中记录到stderr.log
并重定向到描述符3。
描述符3
一直将其记录到combined.log
。 所以, combined.log
包含stdout
和stderr
。
要将stderr重定向到文件,请在屏幕上显示stdout,并将stdout保存到文件:
./aaa.sh 2>ccc.out | tee ./bbb.out
编辑 :要在屏幕上同时显示stderr和stdout并将它们保存到文件,能够使用bash的I / O重定向 :
#!/bin/bash # Create a new file descriptor 4, pointed at the file # which will receive stderr. exec 4<>ccc.out # Also print the contents of this file to screen. tail -f ccc.out & # Run the command; tee stdout as normal, and send stderr # to our file descriptor 4. ./aaa.sh 2>&4 | tee bbb.out # Clean up: Close file descriptor 4 and kill tail -f. exec 4>&- kill %1