Pages

Calculate check digit for EAN-13 barcodes

[inode@brio ~]$ cat chkdigit.sh
#!/usr/local/bin/bash
# http://en.wikipedia.org/wiki/European_Article_Number
oe="13"
while read line; do
for (( j=0; j<${#line}; j++ )); do
chk=$((chk+${line:$j:1}*${oe:$j%2:1}))
done
[[ $((chk%10)) == 0 ]] && digit=0 || digit=$((10-chk%10))
chk=0
echo ${line}${digit}
done

[inode@brio ~]$ cat barcodes.txt
068886641760
200020400298
200020400303
200020400323
200020400324
200020400331
200020400332
200020400381
[inode@brio ~]$ ./chkdigit.sh < barcodes.txt
0688866417608
2000204002983
2000204003034
2000204003232
2000204003249
2000204003317
2000204003324
2000204003812

No comments: