Created
April 6, 2026 07:15
-
-
Save zoonderkins/980bc09ea492db71e897bd4eaf29a485 to your computer and use it in GitHub Desktop.
Chinese-not-show-on-linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| log() { printf '\033[1;32m[INFO]\033[0m %s\n' "$*"; } | |
| warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; } | |
| err() { printf '\033[1;31m[ERR ]\033[0m %s\n' "$*" >&2; } | |
| require_root() { | |
| if [ "${EUID:-$(id -u)}" -ne 0 ]; then | |
| err "請用 sudo 執行:sudo bash $0" | |
| exit 1 | |
| fi | |
| } | |
| backup_file() { | |
| local f="$1" | |
| if [ -f "$f" ]; then | |
| cp -a "$f" "${f}.bak.$(date +%Y%m%d%H%M%S)" | |
| log "已備份 $f" | |
| fi | |
| } | |
| has_locale() { | |
| local target="$1" | |
| locale -a 2>/dev/null | tr '[:upper:]' '[:lower:]' | grep -qx "$(printf '%s' "$target" | tr '[:upper:]' '[:lower:]')" | |
| } | |
| ensure_locales_pkg() { | |
| if ! dpkg -s locales >/dev/null 2>&1; then | |
| log "安裝 locales 套件..." | |
| apt-get update | |
| DEBIAN_FRONTEND=noninteractive apt-get install -y locales | |
| else | |
| log "locales 套件已安裝" | |
| fi | |
| } | |
| enable_en_us_utf8() { | |
| local gen="/etc/locale.gen" | |
| if [ ! -f "$gen" ]; then | |
| warn "$gen 不存在,建立新檔" | |
| touch "$gen" | |
| fi | |
| if grep -Eq '^[# ]*en_US\.UTF-8 UTF-8' "$gen"; then | |
| sed -i 's/^[# ]*en_US\.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' "$gen" | |
| else | |
| printf 'en_US.UTF-8 UTF-8\n' >> "$gen" | |
| fi | |
| log "執行 locale-gen..." | |
| locale-gen | |
| } | |
| choose_best_locale() { | |
| if has_locale "en_US.utf8"; then | |
| printf 'en_US.UTF-8' | |
| return | |
| fi | |
| if has_locale "C.utf8"; then | |
| printf 'C.UTF-8' | |
| return | |
| fi | |
| ensure_locales_pkg | |
| enable_en_us_utf8 | |
| if has_locale "en_US.utf8"; then | |
| printf 'en_US.UTF-8' | |
| return | |
| fi | |
| if has_locale "C.utf8"; then | |
| printf 'C.UTF-8' | |
| return | |
| fi | |
| err "找不到可用 UTF-8 locale,請手動檢查 locale -a" | |
| exit 1 | |
| } | |
| write_system_locale() { | |
| local chosen="$1" | |
| local f="/etc/default/locale" | |
| backup_file "$f" | |
| cat > "$f" <<EOF | |
| LANG=$chosen | |
| LC_ALL=$chosen | |
| EOF | |
| log "已寫入 $f" | |
| } | |
| fix_shell_rc_file() { | |
| local f="$1" | |
| [ -f "$f" ] || return 0 | |
| backup_file "$f" | |
| python3 - "$f" <<'PY' | |
| import re | |
| import sys | |
| from pathlib import Path | |
| path = Path(sys.argv[1]) | |
| text = path.read_text(encoding="utf-8", errors="ignore") | |
| lines = text.splitlines() | |
| new_lines = [] | |
| changed = False | |
| patterns = [ | |
| r'^\s*export\s+LC_ALL=.+$', | |
| r'^\s*export\s+LANG=.+$', | |
| r'^\s*LC_ALL=.+$', | |
| r'^\s*LANG=.+$', | |
| ] | |
| for line in lines: | |
| matched = False | |
| for p in patterns: | |
| if re.match(p, line): | |
| new_lines.append(f'# locale-fix disabled: {line}') | |
| matched = True | |
| changed = True | |
| break | |
| if not matched: | |
| new_lines.append(line) | |
| if changed: | |
| new_lines.append('') | |
| new_lines.append('# locale-fix managed block') | |
| new_lines.append('export LANG=C.UTF-8') | |
| new_lines.append('export LC_ALL=C.UTF-8') | |
| path.write_text('\n'.join(new_lines) + '\n', encoding="utf-8") | |
| print(f"updated {path}") | |
| else: | |
| print(f"no-locale-export-found {path}") | |
| PY | |
| } | |
| fix_user_rcs() { | |
| local user_home | |
| local real_user | |
| real_user="${SUDO_USER:-}" | |
| if [ -n "$real_user" ]; then | |
| user_home="$(getent passwd "$real_user" | cut -d: -f6)" | |
| else | |
| user_home="$HOME" | |
| fi | |
| [ -n "$user_home" ] || return 0 | |
| [ -d "$user_home" ] || return 0 | |
| log "檢查使用者 shell 設定檔:$user_home" | |
| fix_shell_rc_file "$user_home/.bashrc" | |
| fix_shell_rc_file "$user_home/.profile" | |
| fix_shell_rc_file "$user_home/.bash_profile" | |
| fix_shell_rc_file "$user_home/.zshrc" | |
| } | |
| show_status() { | |
| log "目前 /etc/default/locale:" | |
| cat /etc/default/locale || true | |
| echo | |
| log "目前 locale -a:" | |
| locale -a || true | |
| echo | |
| } | |
| main() { | |
| require_root | |
| log "開始檢查 locale 問題..." | |
| show_status | |
| local chosen | |
| chosen="$(choose_best_locale)" | |
| log "選用 locale: $chosen" | |
| write_system_locale "$chosen" | |
| fix_user_rcs | |
| log "套用當前 shell 臨時環境..." | |
| export LANG="$chosen" | |
| export LC_ALL="$chosen" | |
| echo | |
| log "修正完成" | |
| echo | |
| echo "建議你現在執行:" | |
| echo " exec \$SHELL -l" | |
| echo "或直接重新登入 SSH" | |
| echo | |
| echo "之後測試:" | |
| echo " locale" | |
| echo " bash <(curl -Ls https://Check.Place) -H" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment