30 May 2021

Sometimes when I have limited time to do something I check time to time the clock or I work more than I planned.
This behaviour have some disadvantage: I check a lot the clock, distracting me to do a great job, and I work for more time than I planned.

So I created a script that help me :)

countdown.sh

#!/bin/bash

MINUTES=0
HALT_SYSTEM="no"

while [[ ${#} -gt 0 ]]; do
    case "${1}" in
	--minutes)
	    if [ ${#2} -ne 0 ]; then
		MINUTES="${2}"
		if ! [[ ${MINUTES} =~ ^[1-9][0-9]*$ ]]; then
		    echo "Invalid minutes value, must be positive > 0. Please read --help for more information"
		    exit 1
		fi
		shift 2
	    fi
	    ;;
	--halt-system)
	    if [ ${#2} -ne 0 ]; then
		HALT_SYSTEM="${2}"
		if [ "${HALT_SYSTEM}" != "yes" -a "${HALT_SYSTEM}" != "no" ]; then
		    echo "Invalid halt system value  must be equal to yes/no. Please read --help for more information"
		    exit 1
		fi
		shift 2
	    fi
	    ;;
	--help)
	    echo "usage: countdown.sh [--help] [--minutes <positive value>] [--halt-system <yes/no>]"
	    echo "OPTIONS"
	    echo "    --minutes"
	    echo "        Countdown counter minutes."
	    echo ""
	    echo "    --halt-system"
	    echo "        Halt system when time is over yes/no."
	    echo ""
	    echo "    --help"
	    echo "        Read this manual."
	    exit 0
	    ;;
	*)
	    echo "Invalid options found ${1}, please read --help for more information."
	    exit 1
	    ;;
    esac
done


COUNT=${MINUTES}

while [ $COUNT -gt 0 ]
do
	MESSAGE="${COUNT} minutes left."
	echo "${MESSAGE}"
	if [ ${COUNT} -eq 5 -o ${COUNT} -eq 2 ]; then
		# Alert in the last 5/2 minutes using text to speech software.
		spd-say "${MESSAGE} ${MESSAGE}"
	fi
	sleep 60
	COUNT=$((COUNT-1))
done

spd-say "Time is over."

if [ "${HALT_SYSTEM}" == "yes" ]; then
	halt --force
fi

Example of usage

countdown.sh --minutes 6 --halt-system no

References