#!/bin/bash

RES="/tmp/ununun"

doline() {
	a="$1"

	# trois chiffres identiques
	if      [[ ${a[@]:2:1} = ${a[@]:0:1} ]] && \
		[[ ${a[@]:1:1} = ${a[@]:0:1} ]] && \
		[[ ${a[@]:0:1} ]]; then

		echo -n "3${a[@]:0:1}" | tee -a "$RES"
		if [[ ${a[@]:3} ]]; then
			doline "${a[@]:3}"
		fi

	# deux chiffres identiques
	elif    [[ ${a[@]:1:1} = ${a[@]:0:1} ]] && \
		[[ ${a[@]:0:1} ]]; then

		echo -n "2${a[@]:0:1}" | tee -a "$RES"
		if [[ ${a[@]:2} ]]; then
			doline "${a[@]:2}"
		fi

	# un seul chiffre
	elif    [[ ${a[@]:1:1} != ${a[@]:0:1} ]] && \
		[[ ${a[@]:0:1} ]]; then

		echo -n "1${a[@]:0:1}" | tee -a "$RES"
		if [[ ${a[@]:1} ]]; then
			doline "${a[@]:1}"
		fi
	fi
}

doall() {
	while true; do
		doline $(tail -1 "$RES")
		echo "" | tee -a "$RES"
		#read
	done
}

###################################################

echo 1 | tee "$RES"

doall

exit $?

