#! /bin/sh # # MKZIPDIC_KENALL.SH # 日本郵便公式の郵便番号住所CSVから、本システム用の辞書を作成(地域名) # Writtenn by 吉田光佑 on 2026 02 14 # # Usage : mkzipdic.sh -f # -f ... # # # [出力] # 戻り値 # # # # # 初期設定 # --- 変数定義 --- readonly file_ZIPDIC="ken_all.txt" readonly url_ZIPCSVZIP=https://www.post.japanpost.jp/zipcode/dl/oogaki/zip/ken_all.zip readonly flg_SUEXECMODE=1 # --- ファイルパス --- PATH="/usr/local/shellshoccar/bin:$PATH" # --- 終了関数定義(終了前に一時ファイル削除) --- exit_trap() { trap 0 1 2 3 13 14 15 [ -n "${tmpf_zipcsvzip:-}" ] && rm -f $tmpf_zipcsvzip [ -n "${tmpf_zipdic:-}" ] && rm -f $tmpf_zipdic exit ${1:-0} } trap 'exit_trap' 0 1 2 3 13 14 15 # --- エラー終了関数定義 --- error_exit() { [ -n "$2" ] && echo "${0##*/}: $2" 1>&2 exit_trap $1 } # --- テンポラリーファイル確保 --- tmpf_zipcsvzip=$(mktemp -t "${0##*/}.xxxxxxxx") [ $? -eq 0 ] || error_exit 1 'Failed to make temporary file #1' tmpf_zipdic=$(mktemp -t "${0##*/}.xxxxxxxx") [ $? -eq 0 ] || error_exit 2 'Failed to make temporary file #2' # メイン # --- 引数チェック --- flg_FORCE=0 case "${1:-}" in '-f') flg_FORCE=1;; esac # type curl >/dev/null 2>&1 || type wget >/dev/null 2>&1 || { error_exit 3 'No HTTP-GET/POST command found.' } # type unzip >/dev/null 2>&1 || type gunzip >/dev/null 2>&1 || { error_exit 4 'No ZIP extracter command found.' } # timestamp_web=$(if type curl >/dev/null 2>&1; then curl -sLI $url_ZIPCSVZIP else wget -S --spider $url_ZIPCSVZIP | sed 's/^ *//' fi | awk ' BEGIN{ status = 0; d["Jan"]="01";d["Feb"]="02";d["Mar"]="03";d["Apr"]="04"; d["May"]="05";d["Jun"]="06";d["Jul"]="07";d["Aug"]="08"; d["Sep"]="09";d["Oct"]="10";d["Nov"]="11";d["Dec"]-"12"; } /^HTTP\// { status = $2; } /^Last-Modified/i { gsub(/:/, "", $6); ts = sprintf("%04d%02d%02d%06d" ,$5,d[$4],$3,$6); } END { if ((status>=200) && (status<300) && (length(ts)==14)) { print ts; } else { print "NOT_FOUND"; } }' ) [ "$timestamp_web" != 'NOT_FOUND' ] || error_exit 5 'The zipcode CSV file not found on the web' printf '%s\n' "$timestamp_web" | grep -Eq '^[0-9]{14}' [ $? -eq 0 ] || timestamp_web=$(TZ=UTC/0 date +%Y%m%d%H%M%S) while [ $flg_FORCE -eq 0 ]; do [ ! -f "$file_ZIPDIC" ] && break timestamp_local=$(head -n 1 "$file_ZIPDIC" | awk '{print $NF}') printf '%s\n' "$timestamp_local" | grep -Eq '^[0-9]{14}$' || break [ $timestamp_web -gt $timestamp_local ] && break exit 0 done if type curl >/dev/null 2>&1; then curl -s $url_ZIPCSVZIP > $tmpf_zipcsvzip else wget -q -o - $url_ZIPCSVZIP > $tmpf_zipcsvzip fi [ $? -eq 0 ] || error_exit 6 'Failed to download the zipcode CSV file' if type unzip >/dev/null; then unzip -p $tmpf_zipcsvzip elif type gunzip >/dev/null; then gunzip <$tmpf_zipcsvzip 2>/dev/null || { error_exit 7 'No Zip archive extracter found (unzip)' } else error_exit 8 'No Zip archive extracter found (unzip or gunzip)' fi | if type iconv >/dev/null 2>&1; then iconv -c -f CP932 -t UTF-8 elif type nkf >/dev/null 2>&1; then nkf -Sw80 else error_exit 9 'No KANJI convertors found (iconv or nkf)' fi | parsrc.sh | awk '$2~/^3|7|8|9$/' | awk 'BEGIN{z="#"; p="generated"; c="at"; t="'$timestamp_web'"; } # $1!=line {pl();z="";p="";c="";t="";line=$1; } # $2==3 {z=$3; } # $2==7 {p=$3; } # $2==8 {c=$3; } # $2==9 {t=$3; } # END {pl(); } # function pl() {print z,p,c,t; }' | sed 's/(.*//' | sed 's/以下に.*//' > $tmpf_zipdic [ -s $tmpf_zipdic ] || error_exit 10 'Failed to make the zipcode dictionary file' mv $tmpf_zipdic "$file_ZIPDIC" [ "$flg_SUEXECMODE" -eq 0 ] && chmod go+r "$file_ZIPDIC" exit 0