[英]How to store output of a shell script inside a variable in conky?
So I'm getting to get the CPU core temperature using sensors
command.所以我开始使用sensors
命令获取 CPU 核心温度。
Inside conky, I wrote在conky里面,我写了
$Core 0 Temp:$alignr${execi 1 sensors | grep 'Core 0' | awk {'print $3'}} $alignr${execibar 1 sensors | grep 'Core 0' | awk {'print $3'}}
Each second I'm running the exact same command sensors | grep 'Core 0' | awk {'print $3'}
每一秒我都在运行完全相同的命令sensors | grep 'Core 0' | awk {'print $3'}
sensors | grep 'Core 0' | awk {'print $3'}
sensors | grep 'Core 0' | awk {'print $3'}
in two places for exact same output. sensors | grep 'Core 0' | awk {'print $3'}
在两个地方完全相同的 output。 Is there is a way to hold the output inside a variable and use that variable in place of the commands.有没有办法将 output 保存在变量中,并使用该变量代替命令。
conky
does not have user variables. conky
没有用户变量。 What you can do instead is call lua from conky to do this for you.你可以做的是从 conky 调用 lua 来为你做这件事。 The lua language is usually built-in to conky by default, so you need only put some code in a file, include the file in the conky setup file, and call the function. lua 语言通常默认内置在conky 中,因此您只需将一些代码放入文件中,将文件包含在conky 设置文件中,然后调用function。 For example, these shell commands will create a test:例如,这些 shell 命令将创建一个测试:
cat >/tmp/.conkyrc <<\!
conky.config = {
lua_load = '/tmp/myfunction.lua',
minimum_height = 400,
minimum_width = 600,
use_xft = true,
font = 'Times:size=20',
};
conky.text = [[
set ${lua myfunction t ${execi 1 sensors | awk '/^Core 0/{print 0+$3}'}}°C
get ${lua myfunction t}°C ${lua_bar myfunction t}
]]
!
cat >/tmp/myfunction.lua <<\!
vars = {}
function conky_myfunction(varname, arg)
if(arg~=nil)then vars[varname] = conky_parse(arg) end
return vars[varname]
end
!
conky -c /tmp/.conkyrc -o
In the myfunction.lua
file, we declare a function myfunction()
(which needs to be prefixed conky_
so we can call it from conky).在myfunction.lua
文件中,我们声明了一个 function myfunction()
(需要以conky_
为前缀,以便我们可以从 conky 调用它)。 It takes 2 parameters, the name of a variable, and a conky expression.它需要 2 个参数、一个变量的名称和一个 conky 表达式。 It calls conky_parse()
to evaluate the expression, and saves the value in a table vars
, under the name provided by the caller.它调用conky_parse()
来评估表达式,并将值保存在表vars
中,使用调用者提供的名称。 It then returns the resulting value to the caller.然后它将结果值返回给调用者。 If no expression was given, it will return the previous value.如果没有给出表达式,它将返回先前的值。
In the conky.text
the line beginning set
calls myfunction
in lua with the arbitrary name of a variable, t
, and the execi
sensors expression to evaluate, save, and return.在conky.text
中,行开头的set
调用myfunction
中的 myfunction,使用变量的任意名称t
和execi
传感器表达式来评估、保存和返回。 The line beginning get
calls myfunction
to just get the value.以get
开头的行调用myfunction
来获取值。
lua_bar
is similar to exec_bar
, but calls a lua function, see man conky
. lua_bar
类似于exec_bar
,但调用 lua function,见man conky
conky 。 However, it expects a number without the leading +
that exec_bar
accepts, so I've changed the awk
to return this, and have added the °C
to the conky text instead.但是,它需要一个没有exec_bar
接受的前导+
的数字,所以我更改了awk
以返回它,并将°C
添加到 conky 文本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.