Per Erik Strandberg /cv /kurser /blog

We just solved a problem at work by using the already existing ts command. It is located in the moreutils and the man-page says ts - timestamp input [...] ts adds a timestamp to the beginning of each line of input.

An example:

$ #sudo apt-get install moreutils 

$ cat demo.sh 
#!/bin/bash

for x in {1..5}
do
    dur=$(($RANDOM/10000)).$RANDOM
    echo $x - $dur;
    sleep $dur
done

$ ./demo.sh 
1 - 3.18818
2 - 0.4231
3 - 1.28289
4 - 2.25283
5 - 0.18847

$ ./demo.sh | ts '%H:%M:%.S' 
10:54:27.773099 1 - 3.24251
10:54:30.987719 2 - 3.24062
10:54:34.230255 3 - 0.30087
10:54:34.532331 4 - 0.32052
10:54:34.855176 5 - 0.20678


This page belongs in Kategori Programmering