问题描述
我们有一些从Windows中的Git Bash(MINGW64)运行的Groovy脚本。 某些脚本会打印项目符号字符•(或类似字符)。 为了使它起作用,我们设置此变量:
export LC_ALL=en_US.UTF-8
但是,对于某些人来说,这还不够。其控制台打印ΓÇó
而不是•
。
关于如何使其正确打印的任何想法,以及为什么即使在设置LC_ALL变量后仍要打印该消息?
更新
关键部分是Groovy脚本的输出打印不正确,但是普通bash脚本没有问题。
解决方法
查询系统语言环境使用的当前字符映射locale charmap
,并使用重新编码过滤输出以使用兼容的字符映射呈现输出的示例:
#!/usr/bin/env sh
cat <<EOF | recode -qf "UTF-8...$(locale charmap)"
• These are
• UTF-8 bullets in source
• But it can gracefully degrade with recode
EOF
使用charmap=ISO-8859-1
时,其呈现为:
o These are
o UTF-8 bullets in source
o But it can gracefully degrade with recode
使用iconv
代替recode
的替代方法,结果可能会更好。
#!/usr/bin/env sh
cat <<EOF | iconv -f 'UTF-8' -t "$(locale charmap)//TRANSLIT"
• These are
• UTF-8 bullets followed by a non-breaking space in source
• But it can gracefully degrade with iconv
• Europe's currency sign is € for Euro.
EOF
iconv
输出,其语言环境为fr_FR.iso-8859-15@Euro
:
o These are
o UTF-8 bullets followed by a non-breaking space in source
o But it can gracefully degrade with iconv
o Europe's currency sign is € for Euro.