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

Bry*_*son 5 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
Run Code Online (Sandbox Code Playgroud)

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

例如

echo "$OPTARG spent on food" | tee `date %d-%m-%y %H_%M_%S` spendinglogs.log
Run Code Online (Sandbox Code Playgroud)

但我无法想象工作.

iob*_*der 11

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

tsmoreutils包中查看。

如果您正在使用bash,则可以tee将 shell 管道作为文件:

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

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