bash - 将时间戳添加到 tee 的输出,但不是原始输出

标签 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

但我无法想象这会起作用。

最佳答案

编辑:测试并更新了正确的信息

moreutils 查看 ts包。

如果您正在使用 bash,您可以将 tee 作为一个文件发送到 shell 管道:

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

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

关于bash - 将时间戳添加到 tee 的输出,但不是原始输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39239379/

相关文章:

bash - 使用 tee 重定向文件描述符 3

bash - 在 bash 脚本中间切换输出文件重定向

bash 命令跨越多行,中间有几行注释

linux - bash 按 yyyy-mm-dd 在 N 天内 bash 中 2 个日期之间的差异

python - 在 python 中而不是在 shell 中使用 os.system 时出错

ffmpeg - 如何使用 tee 输出 rtmp 和 ffmpeg 命令的一个标准输出?

linux - txt 的统计(出现频率 % ecc..)分析

linux - 为什么 awk 模式匹配不返回结果?

bash - 将标准输出捕获到变量但仍将其显示在控制台中

bash - 管道输出到两个不同的命令