How to generate Luhn Sequential numbers and save them in linux?

1.9k views Asked by At

I was generating (19 digit) sequential numbers like this:

seq 1234496713247997000 1234496713247998000 > seq_numbers.txt

and then validating them online using a luhn checker. Instead of doing a two step approach, How can i do this in a like a bash script for example all in one go, and output only valid numbers to a file?

2

There are 2 answers

6
Inian On BEST ANSWER

I do not claim the truthfulness of the luhn checker algorithm from below. It is a reference from this Rosetta Code page.

You can use it as

#!/bin/bash

function luhn_validate
{
    num=$1
    shift 1

    len=${#num}
    is_odd=1
    sum=0
    for((t = len - 1; t >= 0; --t)) {
        digit=${num:$t:1}

        if [[ $is_odd -eq 1 ]]; then
            sum=$(( sum + $digit ))
        else
            sum=$(( $sum + ( $digit != 9 ? ( ( 2 * $digit ) % 9 ) : 9 ) ))
        fi

        is_odd=$(( ! $is_odd ))
    }

    # NOTE: returning exit status of 0 on success
    return $(( 0 != ( $sum % 10 ) ))
}


for i in $(seq 1234496713247997000 1234496713247998000)
do
    if luhn_validate "$i"; then
        echo "$i is valid"
    else
        echo "$i is not valid"
    fi
done

You can put it up in a script as script.sh and set execute permissions chmod +x script.sh and run as

$ ./script.sh

I was able to make it work on GNU bash, version 4.3.46(2)-release (x86_64-pc-msys) and have not tested it on other systems.

P.S.: Use it at your own risk!

0
Zibri On

Why check them all? Just generate the last digit! It is 3 times faster!

#!/bin/bash
# https://stackoverflow.com/questions/41437080
# answer corrected By Zibri

function luhn_generate_last_digit
{
    num=$1
    shift 1

    len=${#num}

    is_odd=$((1-len%2))
    sum=0
    for((t = len - 1; t >= 0; --t)) {
        digit=${num:$t:1}

        if [[ $is_odd -eq 1 ]]; then
            sum=$(( sum + $digit ))
        else
            sum=$(( $sum + ( $digit != 9 ? ( ( 2 * $digit ) % 9 ) : 9 ) ))
        fi

        is_odd=$(( ! $is_odd ))
    }
    if [ $(( $sum % 10 )) -eq 0 ]; then sum=10;else sum=$(( $sum % 10 ));fi

    # NOTE: returning exit status of 0 on success
    echo -n $(( 10 - $sum  ))
}


for i in $(seq 123449671324799700 123449671324799800)
do
        echo "$i"$(luhn_generate_last_digit "$i")
done