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?
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!