1

在 conky 中,如何在模板中嵌套变量?

例子:

${template2 enp0s25} <- WORKS (fixed string)
${template2 ${gw_iface}} < FAILS (nested variable)
${template2 ${execpi 10 ls -d /sys/class/net/enp* 2> /dev/null | sed -e 's,/sys/class/net/,,'}} <- FAILS (nested variable command)

我也尝试过(但失败了):

${combine ${template2 ${gw_iface}}}
${combine ${template2} ${gw_iface}}

这是“模板2”:

template2 = [[
${if_existing /proc/net/route \1}Wired Ethernet ("\1"):
 - MAC: ${execi 5 cat /sys/class/net/\1/address}    IP: ${addr  \1}
 - Max: ${execi 5 /sbin/ethtool  '\1' 2>/dev/null | sed -n -e 's/^.*Speed: //p'}${goto 190}${if_match ${downspeedf \1} > 0}${font :bold:size=14}${endif}Down: ${downspeedf \1}kB/s${font}${goto 370}${if_match ${upspeedf \1} > 0}${font :bold:size=14}${endif}Up: ${upspeedf \1}kB/s${font}
${endif}]]

谢谢您的帮助。

4

1 回答 1

0

模板有点受限,因为您无法在传递参数之前对其进行评估。一种解决方法是使用一些lua代码显式地执行 eval,然后解析模板。例如,

conky.config = { 
    lua_load = '/tmp/myfunction.lua',
    ...
};
conky.text = [[
 ${lua myeval template2 ${gw_iface}}
]]

创建lua文件,/tmp/myfunction.lua持有

function conky_myeval(tpl, var1)
 v = conky_parse(var1)
 cmd = "${"..tpl.." "..v.."}"
 return conky_parse(cmd)
end

lua函数采用模板的名称tpl和要评估的参数var1conky_parse()它使用例如字符串“xxx”来评估后者,然后构造一个新的字符串"${template2 xxx}",该字符串被解析并作为${lua}调用的值返回。

对于更长的示例${execpi ...}也可以这样做。

于 2021-06-27T14:25:46.720 回答