我有一个 bash 脚本,我在名为 .txt 的文件中定义 UTF-8 希腊符号greek-utfb.sh
。我想运行测试来显示用于打印文件 greek-utfb-scout 中的希腊字母的变量。
--- greek-utfb.sh ----
utfb-greek ()
{
Alph="\u0391" # Alpha
Beta="\u0392" # Beta
Gamm="\u0393" # Gamma
Delt="\u0394" # Delta
Epsi="\u0395" # Epsilon
}
--- greek-utfb-scout.sh ---
greek-utf-scout ()
{
echo -e "Alph: $Alph"
echo -e "Beta: $Beta"
echo -e "Gamm: $Gamm"
echo -e "Delt: $Delt"
echo -e "Epsi: $Epsi"
}
我必须添加什么greek-utfb-scout.sh
才能将希腊语变量翻译为greek-utfb.sh
.
答案1
首先,不要将变量定义放在函数内。相反,更改greek-utfb.sh
为如下所示:
#!/bin/sh
Alph="\u0391" # Alpha
Beta="\u0392" # Beta
Gamm="\u0393" # Gamma
Delt="\u0394" # Delta
Epsi="\u0395" # Epsilon
现在将此文件来源greek-utfb-scout.sh
:
#!/bin/sh
. greek-utfb.sh
greek-utf-scout ()
{
echo -e "Alph: $Alph"
echo -e "Beta: $Beta"
echo -e "Gamm: $Gamm"
echo -e "Delt: $Delt"
echo -e "Epsi: $Epsi"
}
运行后. greek-utfb-scout.sh
,该函数现在按预期工作:
$ . greek-utfb-scout.sh
$ greek-utf-scout
Alph: Α
Beta: Β
Gamm: Γ
Delt: Δ
Epsi: Ε