Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a specific locale is enabled in bash

Tags:

bash

locale

My script needs to run a program with a specific locale to work correctly, so I want it to check whether that locale is available. I've used this hack for now, but I think there's a better way to do this.

grep ^ja_JP /etc/locale.gen &>/dev/null || echo >&2 "enable any of the japanese locales first"
like image 538
mio Avatar asked Sep 21 '13 07:09

mio


People also ask

What is ${ 2 in bash?

$1 , $2 , et al are the command-line arguments to your program (or function). Putting the two together it means to return $2 if there were two arguments passed, otherwise return $1 .

What is $s in bash?

From man bash : -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell. From help set : -e Exit immediately if a command exits with a non-zero status.

What is #* in bash?

The relevant fragment of Bash Reference Manual: here. The syntax has nothing to do with arrays. In particular ${line#* } expands like $line but removes the smallest portion matched by * (asterisk+space, where asterisk matches zero or more characters and space matches space character) from the beginning of it.

What is the meaning of ${ 1 in bash?

$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, $0, $1, $3, $4 and so on. If you run ./script.sh filename1 dir1, then: $0 is the name of the script itself (script.sh)


2 Answers

man locale would tell you that locale -a would list all available locales.

Instead say:

locale -a | grep -q ^ja_JP || echo "enable any of the japanese locales first"
like image 124
devnull Avatar answered Nov 09 '22 18:11

devnull


locale -a should list all the available locales:

if locale -a | grep ^ja_JP ; then
    # locale installed...
else
    # locale not installed...
fi
like image 4
choroba Avatar answered Nov 09 '22 17:11

choroba