在 tmux.conf 中引用一个复杂的 awk 程序

Quoting a complex awk program in tmux.conf

Freenode #tmux 上的一位用户提问:

  1. 我们如何使用 GNU awk for set -g tmux status-right 正确地转义此 shell 命令?

    sensors | awk '/^Physical id 0:/ { s = ; sub(/^+/, "", s); print s; exit }'
    

    结果应该是45.0°C.

  2. 另外,我们如何让它每 30 秒更新一次?

sensors的输出:

coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +45.0°C  (high = +80.0°C, crit = +100.0°C)
...

设置状态-右

在 tmux

中用 shell 命令 #( ) 引用

在 tmux #( ) 中引用很复杂,因为内容是 evaluated twice

因此,让我们将 gawk 程序简化为:

sensors | awk '/^Physical id 0:/ { sub(/^+/, "", ); print ; exit }'

现在我们把它插入.tmux.conf:

set-option -g status-right "#( sensors | awk \' /Physical id 0:/ {  sub\(/\+/,\"\",\); print $4; exit }  \')"

但是下次你去修修补补时阅读和更改它非常复杂...

更简单的选择

最简单的解决方案是将 shell 命令放入文件中并从 tmux 调用它。

~/bin/tmux-status.bash:

#!/bin/bash
sensors | awk '/^Physical id 0:/ { sub(/^+/, "", ); print ; exit }'

~/.tmux.conf:

set-option -g status-right "#(bash ~/bin/tmux-status.bash)"

每 30 秒更新一次

set-option -g status-interval 30

另见