我正在写一个小预算脚本来关注我的财务状况。我想记录我的所有交易以及交易发生的时间。
目前,我输入支出作为参数:
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
但我无法想象这会起作用。
最佳答案
编辑:测试并更新了正确的信息
从 moreutils 查看 ts
包。
如果您正在使用 bash
,您可以将 tee
作为一个文件发送到 shell 管道:
echo "$OPTARG spent on food" | tee >(ts "%d-%m-%y %H_%M_%S" > spendinglogs.log)
我之前的回答正确地陈述了上面的正确答案,但也有一个使用 pee
的错误答案,同样来自 moreutils
。 pee
似乎在将标准输入发送到输出管道之前对其进行缓冲,因此这不适用于时间戳(但它适用于时间不重要的命令)。
关于bash - 将时间戳添加到 tee 的输出,但不是原始输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39239379/