I have this script that should prepend the timestamp to every output line of a command:
$command 2>&1 | while IFS= read -r line; do printf '[%s] %s
' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
It's working fine for most cases, but I have a certain case in which I have an issue.
Let's take for example this:
read -rep "Your age: " age | while IFS= read -r line; do printf '[%s] %s
' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
In this case the timestamp is not prepended.
I understand the problem here is that the newline is not reached so the newline is not printed. I was wondering if is possible to print all the chars as they are received and then print the timestamp once we get a newline.
question from:
https://stackoverflow.com/questions/65922422/prepend-timestamp-to-every-output-line-of-a-command 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…