#! /bin/ksh # # Modulus-10 in Korn-shell / sed. # # Tested on: Tru64 5.1[ab], Solaris 9, Debian GNU/Linux. # # If using the 'bash'-shell, the calls to sed could be # replaced with bash's builtin variable substring # functionality; ${3:0:1} and ${3:1} respectivley. # # (Modulus-10 is used for Credit Card number validation, # in Swedish social security numbers, bank account numbers, # organization numbers and more...) # # # Usage: ./modulus10.ksh # # 28th of August, 2004 # http://netilium.org/~mad/ # Martin Adolfsson # function modulus10 { if [ $# -ne 3 ] ; then modulus10 1 0 "$1" else if [ -z "$3" ] ; then ACCUM="$2" echo $(((($ACCUM+(10-($ACCUM%10)))-$ACCUM)%10)) else NUM="$1" VAL="`echo $3 | sed 's/^\(.\).*/\1/'`" CALC="$(($VAL * ($NUM+1)))" [ "${CALC}" -gt 9 ] && CALC="$((CALC-9))" NUM="$(($NUM + 1))" modulus10 $(($NUM % 2)) $(($CALC + $2)) "`echo $3|sed 's/^.//'`" fi fi } modulus10 "$1"