如何在 Bash 脚本中为 conky 制作特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8334266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:07:32  来源:igfitidea点击:

How to make special characters in a Bash script for conky?

bashshellutf-8special-characters

提问by mountpeaks

I wonder how can I make a Celsius character in my bash script, so I could correct the output in "CPU temp: 50 C" line? What are the escape sequences for this kind of special characters, providing my font supports them?

我想知道如何在我的 bash 脚本中创建一个摄氏度字符,以便我可以更正“CPU temp:50 C”行中的输出?这种特殊字符的转义序列是什么,前提是我的字体支持它们?

Still scratching my head...

还在挠头……

To be more precise I need it to use in conky I use this line: awk '{printf($3" C")}'. How should I correct it so that it outputs Celsius degree? Cause if I insert ℃ character directly, conky screws it.

更准确地说,我需要在 conky 中使用它,我使用这一行:awk '{printf($3" C")}'。我应该如何纠正它以输出摄氏度?因为如果我直接插入℃字符,conky会把它拧紧。

回答by Adam Zalcman

All non-control sequences of bytes your script outputs to the terminal are interpreted for display according to the terminal's settings. If you configure it to interpret incoming data as utf-8then all you need to do in your bash script is output the Celsius character as required by utf-8.

您的脚本输出到终端的所有非控制字节序列都将根据终端的设置进行解释以显示。如果您将其配置为将传入数据解释为utf-8,那么您需要在 bash 脚本中执行的所有操作就是输出utf-8所需的摄氏度字符。

Setting your terminal right depends on the application you're using. It is quite likely that it is already in utf-8 mode.

正确设置终端取决于您使用的应用程序。它很可能已经处于 utf-8 模式。

In order to display the character you want you need to look up its Unicode codepoint and encode it in utf-8 or you can look it up somewhere where character's utf-8 sequence is already shown. Celsius character is described herefor example.

为了显示您想要的字符,您需要查找其 Unicode 代码点并将其编码为 utf-8,或者您可以在已经显示字符的 utf-8 序列的地方查找它。例如,这里描述摄氏度字符。

Celsius utf-8 sequence is 0xE2 0x84 0x83, so you can display it in your shell using the $'string'construct since it accepts the \xhhescape:

摄氏 utf-8 序列是 0xE2 0x84 0x83,因此您可以使用该$'string'构造在 shell 中显示它,因为它接受\xhh转义:

$ echo $'\xe2\x84\x83'
℃

or echo with -e:

或者用 -e 回显:

$ echo -e '\xe2\x84\x83'
℃

Also, you can assign it to a variable

此外,您可以将其分配给变量

$ CEL=$'\xe2\x84\x83'
$ echo $CEL
℃

If you wish to display ℃ as two separate characters: degree character and the C letter then you can use this characterand this command:

如果您希望将 ℃ 显示为两个单独的字符:度数字符和 C 字母,那么您可以使用这个字符和这个命令:

$ echo $'\xc2\xb0'C
°C

If you're using a shell which doesn't support the $'string'construct, you can also use utilities like perl or python:

如果您使用的 shell 不支持该$'string'构造,您还可以使用 perl 或 python 等实用程序:

$ python -c 'print "\xe2\x84\x83"'
℃
$ perl -e 'print "\xe2\x84\x83\n"'
℃

In GNU awkyou can use standard C language escapes including \xhh, for example:

在 GNU 中,awk您可以使用标准的 C 语言转义,包括\xhh,例如:

$ awk 'BEGIN { print "\xe2\x84\x83"; }' 
℃
$ awk 'BEGIN { print "\xc2\xb0\C"; }' 
°C

Note that an extra backslash was needed to terminate the hexadecimal number (without it the escape sequence consumes all hexadecimal digits including letter C, see GNU awk user's guide).

请注意,需要额外的反斜杠来终止十六进制数(没有它,转义序列会消耗所有十六进制数字,包括字母 C,请参阅GNU awk 用户指南)。

More portable solution in awkis to use octal sequence. Since hexadecimal 0xC2 is octal 302 and hexadecimal 0xB0 is octal 260, you can do the following:

更便携的解决方案awk是使用八进制序列。由于十六进制 0xC2 是八进制 302 而十六进制 0xB0 是八进制 260,您可以执行以下操作:

$ awk 'BEGIN { print "20C"; }'
°C

Similarly,

相似地,

$ awk 'BEGIN { print "243"; }'
℃

回答by ratbum

There isn't an escape sequence for ° as far as I'm aware.

据我所知,° 没有转义序列。

回答by Beginner

You can try this:

你可以试试这个:

echo -e "CPU temp: 50\xE2\x84\x83"

Related: How do you echo a 4-digit Unicode character in Bash?

相关:如何在 Bash 中回显 4 位 Unicode 字符?