Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #!/bin/bash
  2. getchecknumber(){ # get the last digit of the cc number
  3. str=$1
  4. checknum=$(echo ${str: -1})
  5. }
  6. getrest(){ # get the rest of the numbers in the sequence
  7. str=$(echo ${str%?})
  8. }
  9. doubleeveryother(){ # double every other number
  10. # split characters into an array
  11. resetcolor
  12. arr=()
  13. i=0
  14. while [ "$i" -lt "${#str}" ]; do
  15. arr+=(${str:$i:1})
  16. i=$((i+1))
  17. done
  18. for ((i=1; i<=15; i++))
  19. do {
  20. char=$(echo "$i-1" | bc)
  21. double=$(echo "${arr[$char]} * 2" | bc)
  22. if [ $(($i%2)) -eq 0 ] ; then
  23. foo=bar
  24. else
  25. arr[$char]=$double #replace the number in the array
  26. fi
  27. }
  28. done
  29. echo -e
  30. }
  31. adddigitsum() { #split a two digit number into two numbers and add them
  32. for ((a=0; a<=14; a++))
  33. do {
  34.  
  35. NUMSPLIT=()
  36. number=$(echo ${arr[$a]})
  37. if [[ ${#number} == 2 ]] ; then
  38. i=0
  39. while [[ "$i" -lt ${#number} ]]; do
  40. NUMSPLIT+=(${number:$i:1})
  41. i=$((i+1))
  42. done
  43. num=$((${NUMSPLIT[0]} + ${NUMSPLIT[1]}))
  44. addeddigit=$( echo $num | bc )
  45. arr[$a]=$addeddigit # replace the number in the array
  46. fi
  47. }
  48. done
  49. }
  50. addall(){ #add all the digits
  51. sum=$( IFS="+"; bc <<< "${arr[*]}" )
  52. }
  53. multiply(){ #multiply by 9
  54. result=$( echo "$sum * 9" | bc )
  55. }
  56. validate(){ #see if the last number of 'result' is equal to the checknum
  57. resultchecknum=$(echo ${result: -1})
  58. if [[ $resultchecknum == $checknum ]] ; then
  59. color 046
  60. echo "CC Number is valid!"
  61. else
  62. color 196
  63. echo "CC Number is not valid."
  64. fi
  65. }
  66. echo "$1 "
  67. getchecknumber $1
  68. getrest
  69. doubleeveryother
  70. adddigitsum
  71. addall
  72. multiply
  73. validate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement