添加时间戳到tee'd输出,但不是原始输出

时间:2016-08-31 01:26:34

标签: bash tee

我正在写一些预算脚本来关注我的财务状况。我想记录我的所有交易以及它们何时发生。

目前,我将支出作为参数输入:

f)
    echo "$OPTARG spent on food" | tee spendinglogs.log
    ... # take away money from monthly budget
    echo "$REMAINING_FOOD_BUDGET remaining" | tee spendinglogs.log

m)
    echo "$OPTARG spent on miscellaneous" | tee spendinglogs.log
    ... # take away money from monthly budget
    echo "$REMAINING_MISC_BUDGET remaining" | tee spendinglogs.log

... #etc

我不想将输出时间戳到终端,但我确实想要将输出时间戳记到日志中。有没有办法做到这一点?

例如

echo "$OPTARG spent on food" | tee `date %d-%m-%y %H_%M_%S` spendinglogs.log

但我无法想象有效。

2 个答案:

答案 0 :(得分:2)

编辑:使用正确的信息进行测试和更新

moreutils包中查看ts

如果您使用的是bash,则可以将tee作为文件发送到shell管道:

echo "$OPTARG spent on food" | tee >(ts "%d-%m-%y %H_%M_%S" > spendinglogs.log)

我之前的回答正确地陈述了上述正确答案,但使用pee时也是错误的答案,也来自moreutilspee似乎在将stdin发送到输出管道之前对其进行缓冲,因此这不适用于时间戳(它将适用于时间不重要的命令)。

答案 1 :(得分:0)

试试这个:

echo something 2>&1 | while read line; do echo $line; echo "$(date) $line" >> something.log; done
相关问题