简体   繁体   English

如何编辑 conky 模板以动态显示附加的 USB 设备?

[英]How do I edit conky templates to dynamically show attached USB devices?

I have followed the example from Casey's Conky Reference with Examples .我遵循了Casey 的 Conky Reference with Examples中的示例。

I modified it to have我修改它有


[conky config section]
 -- Format the storage information the same way for every device. Parameters: [human name] [path]
template3 = "\\1 [${fs_size \\2} ${fs_type \\2}]${alignr}${fs_used \\2} used ${fs_bar 10,60 \\2}\n${alignr}${voffset -15}${fs_used_perc \\2}%\nMount=${color3}\\2$color",

[conky TEXT section]
${execpi 60 df -h -t ext4 -t vfat -t fuse.sshfs -t fuseblk -t hfsplus --output=source,target | grep  -ve "boot" -ve "nvme0"  -ve "sda"  | grep '^/dev/' | cut --characters=6- | sed 's/^/\$\{template3 /;s/$/\}/'}

which is working.这是工作。

I would like to separate out the name of the USB device and display it, instead of displaying the entire path of the mount point.我想将 USB 设备的名称分离出来并显示出来,而不是显示挂载点的整个路径。

So, I have tried the lines below:所以,我尝试了以下几行:

[conky config section]
template3 = "\\1 [${fs_size \\2} ${fs_type \\2}]${alignr}${fs_used \\2} used ${fs_bar 10,60 \\2}\n${alignr}${voffset -15}${fs_used_perc \\2}%\nName=${color3}\\$color",

[conky TEXT section]
${execpi 60 df -h -t ext4 -t vfat -t fuse.sshfs -t fuseblk -t hfsplus --output=source,target | grep  -ve "boot" -ve "nvme0"  -ve "sda"  | grep '^/dev/' | awk -F/ '{printf "%s\t%s\n", $0, $NF}' | cut --characters=6- | sed 's/^/\$\$\{template3 /;s/$/$/\}/'}


I don't know how to get that to work, maybe because I am not very good with sed .我不知道如何让它发挥作用,可能是因为我对sed不是很好。

You need to add a 3rd argument to the template, and refer to it with \\3 .您需要向模板添加第三个参数,并使用\\3引用它。 So you change it to:因此,您将其更改为:

template3 = "...Name=${color3}\\3$color"

This 3rd argument must be separated by spaces when the template is actually used, not by the tab \t you have used in the added awk .实际使用模板时,此第三个参数必须用空格分隔,而不是您在添加的awk中使用的制表符\t分隔。 You didn't need to change the sed, which just surrounds the output with ${template3... } .您不需要更改 sed,它只是用${template3... }围绕 output 。

Instead of mixing grep, awk, and sed, you can do it all in awk more easily.无需混合 grep、awk 和 sed,您可以在 Z5E4C8ZEFA9E205687E27F26F 中更轻松地完成所有操作Try:尝试:

${execpi 60 df -h -t ext4 -t vfat -t fuse.sshfs -t fuseblk \
  -t hfsplus --output=source,target |
 awk -F/ '/boot|nvme0|sda/{ next }
          $0 ~ "^/dev/" { printf "${template3 %s %s}\n",substr($0,6),$NF }'}

The original grep -ve "boot" means match lines not containing "boot".原来的grep -ve "boot"表示匹配行包含 "boot"。 The awk equivalent would be /boot/{ next } , that is: if you match the regular expression "boot" in the input line, just go on to handle the next input line instead. awk 等效项将是/boot/{ next } ,即:如果您匹配输入行中的正则表达式“boot”,则只需 go on 来处理下一个输入行。 The regular expression /boot|nvme0|sda/ means boot or nvme0 or sda .正则表达式/boot|nvme0|sda/表示bootnvme0sda

The original grep '^/dev/' uses a regular expression which means only match lines beginning ( ^ ) with /dev/ .原始grep '^/dev/'使用正则表达式,这意味着仅匹配以/dev/开头的行( ^ )。 The awk equivalent can be /^\/dev\//{print} , but since regular expressions must be in // this means you need to escape the / and can be a bit less readable than the alternative: $0 ~ "^/dev/" {....} , where $0 is the whole line, ~ means match, and you can use a string in quotes as a regular expression. awk 等效项可以是/^\/dev\//{print} ,但由于正则表达式必须在//这意味着您需要转义/并且可能比替代方案的可读性差一些: $0 ~ "^/dev/" {....} ,其中$0是整行, ~表示匹配,您可以使用引号中的字符串作为正则表达式。

The original cut... means from the 6th character, and awk has a function substr(string, start, length) to make a sub-string that can do that.原始cut...表示从第 6 个字符开始,并且 awk 有一个 function substr(string, start, length)来制作一个可以做到这一点的子字符串。 If no length is given, the whole of the rest of the string is returned.如果没有给出长度,则返回字符串的整个 rest。

You are allowed newlines in ${exec} forms, but you must be careful with templates: use an artifice to ensure ${template3 never occurs in the string.您可以在${exec} forms 中使用换行符,但您必须小心使用模板:使用技巧来确保${template3永远不会出现在字符串中。 Here I used $\{ .在这里,我使用$\{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM