diff options
author | David Wührer <def@gmx.at> | 2024-03-17 11:55:29 +0100 |
---|---|---|
committer | David Wührer <def@gmx.at> | 2024-03-17 11:55:29 +0100 |
commit | 75b23ce1ff0daab363725a2a5755ea80bbeaa68e (patch) | |
tree | 43c0965b1deb621cdab18cde2868c83beaa56aee | |
parent | 8348c1c4fd096e853624f7b6758fa10bc7b6cb74 (diff) |
ean13
-rwxr-xr-x | ean13 | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -0,0 +1,22 @@ +#!/bin/bash +while [[ $1 ]] +do +[[ ${#1} -eq 13 ]] || [[ ${#1} -eq 12 ]] || { + printf >&2 'length is %d, should be 13\n' ${#1} + exit 1 +} +declare -i checksum=0 +declare -i factor=1 +for i in {0..11} +do + v=${1:i:1} + checksum=$((checksum+factor*v)) + factor=$((factor^2)) +#printf >&2 'v: %s : cs=%d f=%d\n' "$v" $checksum $factor +done +if [[ ${#1} -eq 12 ]] +then echo "$1"$((10-checksum%10)) +else [[ $((10-checksum%10)) -eq ${1:12} ]] || exit +fi +shift +done |