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"
$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 .
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.
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.
$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)
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"
locale -a
should list all the available locales:
if locale -a | grep ^ja_JP ; then
# locale installed...
else
# locale not installed...
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With