ts is a Perl script from moreutils. You use it with pipe, it receives stdout of another program. The basic idea is doing:
command | ts
It’s useful when you need to know when a program processes something. But it’s not enough for me, I wrote a Bash script ts.sh to do more.
#!/bin/bash DATECMD='date +%H:%M:%S' process_stdout() { while read line; do echo -e "\e[44m$($DATECMD)\e[0m $line" done } process_stderr() { while read line; do echo -e "\e[41m$($DATECMD)\e[0m $line" 1>&2 done } if (( $# == 0 )); then process_stdout else exec 3>&1 ( bash -c "$*" | process_stdout ) 2>&1 1>&3 | process_stderr exec 3>&- fi
It can timestamp stdout and stderr of a command with different color. See the following example:
The timestamps on red is the stderr of emerge, the rest on blue is the stdout. This script could also be used with pipe, but it would only timestamp on stdin, which is a command’s stdout via pipe. It’s best to use it like:
ts.sh command arg1 arg2 ...
An example with shell script as command:
ts.sh while true \; do echo tik tok \; sleep 1 \; done
Thanks for this useful script!
ReplyDelete