Skip to content

Instantly share code, notes, and snippets.

@redmcg
Last active February 28, 2024 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save redmcg/6444516692c7ee72d1a79e1a98ca75dc to your computer and use it in GitHub Desktop.
Save redmcg/6444516692c7ee72d1a79e1a98ca75dc to your computer and use it in GitHub Desktop.
LUHN Check in Bash
#!/usr/bin/env bash
. library.sh
pan="$1"
isLUHNValid "$pan"
isLUHNValid() {
local pan="$1"
local panlen="${#pan}"
local sum=0
for ((i = panlen - 1; i >= 0; i--)); do
local digit="${pan:$i:1}"
if (((panlen-i) % 2 == 0)); then
#even
((digit*=2))
((${#digit} == 2)) && digit=$((${digit:0:1}+${digit:1:1}))
fi
((sum+=digit))
done
((sum % 10 == 0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment