So far I have found that Termux is the only POSIX environment without POSIX locale; as a result the following command, for example:
awk 'BEGIN{for(i=1;i<256;i++)printf"%c",i;}'
outputs not every non-null byte; the awk on Termux is actually GNU awk and it seems to depend on locale. The -b
option, however, makes it succeed.
On issue for ShellShoccar-jpn/kotoriotoko (Japanese) we are discussing how to handle Japanese and other UTF-8 strings proparely, but at this point the discussion is going somewhere out of compatibility; the latest idea doesn't think of what if GAWK is available but POSIX locale and UTF-8 locales are not available. Also what about non-GAWK awk? Maybe awk '...' | xargs -I x printf x
is the most compatible idea.
First, how to detect that POSIX locale is not available? So far I have thought of these (never tested yet) (also I have found out that Termux does never serve locale
command nor POSIX locale; only en_US.UTF-8
is available):
# 1
POSIX_LOCALE_AVAILABLE=no
type locale >/dev/null 2>&1 && {
locale -a |
grep -qE '^(C|POSIX)$' &&
POSIX_LOCALE_AVAILABLE=yes
}
# 2
export LC_ALL=C
POSIX_LOCALE_AVAILABLE=no
case "$LC_ALL" in ('C')
POSIX_LOCALE_AVAILABLE=yes
;;esac
# 3
POSIX_LOCALE_AVAILABLE=no
case "$(
LC_ALL=C awk 'BEGIN{for(i=1;i<256;i++)printf"%c",i;}' |
od -A n -t x1 -v |
tr ABCDEF abcdef |
tr -Cd abcdef1234567890
)" in ("$(
awk 'BEGIN{for(i=1;i<256;i++)printf"%02x",i;}'
)")
POSIX_LOCALE_AVAILABLE=yes
;;esac
But do they work on every POSIX environment? If not, any alternatives?
LC_ALL=C
, the shell doesn’t restrict the values that can be specified.getconf PATH
. I cannot see a dependence to GAWK since you do not use the GNU option-b
in the scripts. It would help, if you did explain your exact problems.