检查是否在bash中启用了特定的区域设置

检查是否在bash中启用了特定的区域设置,bash,locale,Bash,Locale,我的脚本需要运行具有特定区域设置的程序才能正常工作,因此我希望它检查该区域设置是否可用。我已经用过这个方法了,但我认为有更好的方法 grep ^ja_JP /etc/locale.gen &>/dev/null || echo >&2 "enable any of the japanese locales first" locale-a应列出所有可用的locale: if locale -a | grep ^ja_JP ; then # locale ins

我的脚本需要运行具有特定区域设置的程序才能正常工作,因此我希望它检查该区域设置是否可用。我已经用过这个方法了,但我认为有更好的方法

grep ^ja_JP /etc/locale.gen &>/dev/null || echo >&2 "enable any of the japanese locales first"

locale-a
应列出所有可用的locale:

if locale -a | grep ^ja_JP ; then
    # locale installed...
else
    # locale not installed...
fi

manlocale
将告诉您
locale-a
将列出所有可用的locale

而是说:

locale -a | grep -q ^ja_JP || echo "enable any of the japanese locales first"

不幸的是,locale-a不足以验证配置选项,因为它会拒绝有效的案例。例如,我可以使用“en_CA.utf8”和“en_CA.UTF-8”达到相同的效果,但后者失败,因为locale-a列出了前者。当编写要在不同环境中部署的代码时,这是特别不可取的,但到目前为止,我还找不到任何其他方法来验证配置选择。