简体   繁体   English

如何将 shell 脚本的 output 存储在 conky 的变量中?

[英]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,使用变量的任意名称texeci传感器表达式来评估、保存和返回。 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.

相关问题 如何在 shell 脚本中使用空格格式化 output? - How to format output with spaces inside a shell script? 如何在 Linux shell 脚本中声明一个存储管道命令输出的变量 - How to declare a variable which store pipe command output in Linux shell script 如何在shell脚本中将shell变量传递给awk命令 - How to pass shell variable to awk command inside shell script 如何将变量设置为shell脚本中变量内的变量 - How to set a variable to the variable inside a variable in a shell script 从另一个脚本调用一个Shell脚本并将该脚本的输出存储在变量中 - Call one shell script from another script and store that output of that script in a variable 我如何将 shell 命令 output 存储到 python 中的变量? - How would I store the shell command output to a variable in python? 如何读取 json 文件并将值存储到 shell 脚本中的某个变量 - How to read json file and store value to some variable in shell script 如何将命令存储在 shell 脚本的变量中? - How can I store a command in a variable in a shell script? 如何在Shell脚本中将uniq命令的输出分配给变量 - How to assign the output of uniq command to a variable in shell script 如何在Shell脚本中的变量中捕获te.net命令的output - How to capture the output of telnet command in a variable in Shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM