Skip to content

Instantly share code, notes, and snippets.

@Sea-n
Last active January 21, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sea-n/eec18ce358b5680478f69c8245ddc845 to your computer and use it in GitHub Desktop.
Save Sea-n/eec18ce358b5680478f69c8245ddc845 to your computer and use it in GitHub Desktop.
身份證字號驗證器

台灣身份證字號驗證 (Shell Script)

Install

  • 安裝至 local
    1. wget -q -O /usr/local/bin/verify https://git.io/DL
    2. chmod 755 /usr/local/bin/verify
  • 直接使用
    1. wget -q -O verify.sh https://git.io/DL
    2. chmod +x verify.sh

Usage

  1. 驗證單一號碼 verify A123456789
  2. 驗證大量號碼 cat FILE FILE2 FILE3 |xjobs -j32 -v0 verify 僅顯示錯誤號碼
#!/bin/bash
id=$1 # A123456789
declare -A sumTable=([A]=1 [B]=0 [C]=9 [D]=8 [E]=7 [F]=6 [G]=5 [H]=4 [I]=9 [J]=3 [K]=2 [L]=2 [M]=1 [N]=0 [O]=8 [P]=9 [Q]=8 [R]=7 [S]=6 [T]=5 [U]=4 [V]=3 [W]=1 [X]=3 [Z]=0)
if [[ ! $1 =~ ^[A-Z][12][0-9]{8}$ ]]; then
exit 255
fi
no0=${id:0:1} # A
no1=${id:1:1} # 1
no2=${id:2:1} # 2
no3=${id:3:1} # 3
no4=${id:4:1} # 4
no5=${id:5:1} # 5
no6=${id:6:1} # 6
no7=${id:7:1} # 7
no8=${id:8:1} # 8
no9=${id:9:1} # 9
sum=$((${sumTable[$no0]} + $no1*8 + $no2*7 + $no3*6 + $no4*5 + $no5*4 + $no6*3 + $no7*2 + $no8 + $no9)) # Up to 342
check=$(((350 - $sum) % 10))
if [[ $check == 0 ]]; then
exit 0
else
echo ERROR: $id
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment