问题描述
@H_404_0@以下代码片段为时间戳添加了稍稍不同的stdout和stderr字符串。那不是我想要的。如何修改以仅将时间戳添加到日志文件,同时保持输出到屏幕的原样?
LOGFILE=/var/log/a.log
exec 1> >(stdbuf -e0 -o0 ts '[%F %T] O:' | tee -a "$LOGFILE") \
2> >(stdbuf -e0 -o0 ts '[%F %T] E:' | tee -a "$LOGFILE" >&2)
解决方法
使用过程替换代替tee
参数,而不是直接使用文件。示例:
#!/bin/bash
cat file | tee >(ts >> log.txt)
> bash test.sh
test1
test2
> cat log.txt
Oct 22 19:34:33 test1
Oct 22 19:34:33 test2
在您的示例中,您首先将输出重定向到ts
,然后再重定向到tee
,因此您也将时间戳记添加到控制台中。