In the following script I can't get the variable STARTPOINT to increment by 3 each time through the loop. I con't get it because the variable NUMBEROFGROUPS is incrementing by -1 each time and both increment lines look identical to me.
#!/bin/bash
set -x
leading_zeros() {
local NUM=${1}
local NUMBEROFDIGITS=${2}
local NUMBEROFGROUPS=${3}
while [[ $(( ${#NUM} / 3 )) -ne ${NUMBEROFGROUPS} ]] || [[ ${#NUM} -lt 3 ]]; do
NUM="0${NUM}"
done
echo ${NUM}
}
read -p "Enter a number no higher than the billions range" NUM
NUMBEROFDIGITS=${#NUM}
NUMBEROFGROUPS=$( expr ${NUMBEROFDIGITS} / 3 )
if [[ ${NUMBEROFDIGITS} -lt 3 ]]; then # {
NUMBEROFGROUPS=1
NUM=$(leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
elif [[ `expr ${NUMBEROFDIGITS} % 3` -ne 0 ]]; then # } {
let NUMBEROFGROUPS=NUMBEROFGROUPS+1
NUM=$(leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
fi # }
while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
STARTPOINT=0
CURRENTGROUP=${NUM:${STARTPOINT}:3}
echo ${CURRENTGROUP}
let STARTPOINT=$(( ${STARTPOINT} + 3 ))
let NUMBEROFGROUPS=$(( ${NUMBEROFGROUPS} -1 ))
done # }