# Randomly generating invoice IDs

As more fully detailed in my question here, I'm in the process of starting a small, local business and wanted to avoid showing sequential invoice IDs to clients, and instead generate completely random ones. Thanks to the advice in the comments to that question, I decided to eliminate the risk of any collisions by incorporating logic that checks against a database (a flat text file) of all IDs that have previously been generated.

This is my attempt at doing so, which to my amazement is functional, but probably not as robust as it could be. Since then I have also decided on using 5-digit numbers, as these look better to me and a limit of 99,000 transactions (without 00000, and I might even exclude the first 10,000 digits through to 09999) will be more than enough for me, and the logic of the script means that collisions should no longer be an issue.

#!/usr/bin/env bash

generate_num() {
num=$(head /dev/urandom | tr -dc '[:digit:]' | cut -c 1-5) } read -p "Are you sure you want to generate a new invoice ID? [Y/n] " -n 1 -r echo if [[$REPLY =~ ^[Yy]$]] then generate_num && echo Generating a random invoice ID and checking against database... sleep 2 while grep -xq "$num" "ID_database"
do
echo Invoice ID \#$num already exists in database... sleep 2 generate_num && echo Generating new random invoice ID and checking against database... sleep 2 done echo Generated random invoice ID \#$num
sleep 1
echo Invoice ID \#$num does not exist in database... sleep 2 echo$num >> "ID_database" && echo Successfully added Invoice ID \#\$num to database...
fi