Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- getchecknumber(){ # get the last digit of the cc number
- str=$1
- checknum=$(echo ${str: -1})
- }
- getrest(){ # get the rest of the numbers in the sequence
- str=$(echo ${str%?})
- }
- doubleeveryother(){ # double every other number
- # split characters into an array
- resetcolor
- arr=()
- i=0
- while [ "$i" -lt "${#str}" ]; do
- arr+=(${str:$i:1})
- i=$((i+1))
- done
- for ((i=1; i<=15; i++))
- do {
- char=$(echo "$i-1" | bc)
- double=$(echo "${arr[$char]} * 2" | bc)
- if [ $(($i%2)) -eq 0 ] ; then
- foo=bar
- else
- arr[$char]=$double #replace the number in the array
- fi
- }
- done
- echo -e
- }
- adddigitsum() { #split a two digit number into two numbers and add them
- for ((a=0; a<=14; a++))
- do {
- NUMSPLIT=()
- number=$(echo ${arr[$a]})
- if [[ ${#number} == 2 ]] ; then
- i=0
- while [[ "$i" -lt ${#number} ]]; do
- NUMSPLIT+=(${number:$i:1})
- i=$((i+1))
- done
- num=$((${NUMSPLIT[0]} + ${NUMSPLIT[1]}))
- addeddigit=$( echo $num | bc )
- arr[$a]=$addeddigit # replace the number in the array
- fi
- }
- done
- }
- addall(){ #add all the digits
- sum=$( IFS="+"; bc <<< "${arr[*]}" )
- }
- multiply(){ #multiply by 9
- result=$( echo "$sum * 9" | bc )
- }
- validate(){ #see if the last number of 'result' is equal to the checknum
- resultchecknum=$(echo ${result: -1})
- if [[ $resultchecknum == $checknum ]] ; then
- color 046
- echo "CC Number is valid!"
- else
- color 196
- echo "CC Number is not valid."
- fi
- }
- echo "$1 "
- getchecknumber $1
- getrest
- doubleeveryother
- adddigitsum
- addall
- multiply
- validate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement