Last active
February 28, 2024 23:09
-
-
Save redmcg/6444516692c7ee72d1a79e1a98ca75dc to your computer and use it in GitHub Desktop.
LUHN Check in Bash
This file contains 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 | |
. library.sh | |
pan="$1" | |
isLUHNValid "$pan" |
This file contains 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
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