coreutils の ts コマンドが使えないときに、それっぽい出力を得る方法です
タイムスタンプを付与しつつバッファリングしないようにすると見た目が良いので awk を使います
コマンド出力結果をパイプで繋ぎます(coreutils の ts コマンドと似たような使い勝手です)
# エイリアス登録(ダブルクオーテーションで括る例)
$ alias ts="awk '{print strftime(\"[%F %T] \") \$0; fflush() }'"
# 確認(シングルクォーテーションで登録するならこっち)
$ alias ts
alias ts='awk '\''{print strftime("[%F %T] ") $0; fflush() }'\'''
出力例
# ls にタイムスタンプ
$ ls | ts
[2021-03-02 07:56:17] 1000202334
[2021-03-02 07:56:17] Desktop
[2021-03-02 07:56:17] Downloads
# ping にタイムスタンプ
$ ping -c1 192.168.10.1 | ts
[2021-03-02 07:56:31] PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data.
[2021-03-02 07:56:31] 64 bytes from 192.168.10.1: icmp_seq=1 ttl=63 time=2.96 ms
[2021-03-02 07:56:31]
[2021-03-02 07:56:31] --- 192.168.10.1 ping statistics ---
[2021-03-02 07:56:31] 1 packets transmitted, 1 received, 0% packet loss, time 0ms
[2021-03-02 07:56:31] rtt min/avg/max/mdev = 2.960/2.960/2.960/0.000 ms
# sudo も大丈夫
# 最初の3行は標準エラー出力なのでタイムスタンプは付きません
$ sudo apt update | ts
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
[2021-03-02 07:56:43] Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
[2021-03-02 07:56:43] Hit:2 http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04 InRelease
[2021-03-02 07:56:43] Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
[2021-03-02 07:56:44] Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease
[2021-03-02 07:56:44] Hit:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease
[2021-03-02 07:56:45] Reading package lists...
[2021-03-02 07:56:45] Building dependency tree...
[2021-03-02 07:56:45] Reading state information...
[2021-03-02 07:56:45] 3 packages can be upgraded. Run 'apt list --upgradable' to see them.
標準エラーにタイムスタンプを付けるのも簡単です
# 標準エラーをリダイレクトしてタイムスタンプを付与
$ sudo apt update 2>&1 | ts
[2021-03-02 08:06:52]
[2021-03-02 08:06:52] WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
[2021-03-02 08:06:52]
[2021-03-02 08:06:53] Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
[2021-03-02 08:06:53] Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
(略)
time コマンドのように、コマンドを後置します
ts(){
eval "$@" | awk '{print strftime("[%F %T] ") $0; fflush() }'
}
出力例
# 確認
$ type ts
ts is a function
ts ()
{
eval "$@" | awk '{print strftime("[%F %T] ") $0; fflush() }'
}
# ls にタイムスタンプ
$ ts ls
[2021-03-02 07:58:42] 1000202334
[2021-03-02 07:58:42] Desktop
[2021-03-02 07:58:42] Downloads
# ping にタイムスタンプ
$ ts ping -c1 192.168.10.1
[2021-03-02 07:58:50] PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data.
[2021-03-02 07:58:50] 64 bytes from 192.168.10.1: icmp_seq=1 ttl=63 time=3.90 ms
[2021-03-02 07:58:50]
[2021-03-02 07:58:50] --- 192.168.10.1 ping statistics ---
[2021-03-02 07:58:50] 1 packets transmitted, 1 received, 0% packet loss, time 0ms
[2021-03-02 07:58:50] rtt min/avg/max/mdev = 3.896/3.896/3.896/0.000 ms
# sudo の例
$ ts sudo apt update
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
[2021-03-02 07:58:55] Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
[2021-03-02 07:58:55] Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
[2021-03-02 07:58:55] Hit:3 http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04 InRelease
(略)