This is a timer I made quite some time ago and just came across it again today. I'm just wondering if there's anything wrong with it or anything I could improve.
timer() { # Countdown timer
let "hoursToGo = 0"
let "minutesToGo = 0"
let "secondsToGo = 0"
convertHours(){
let "hoursToGo = $1"
}
convertMinutes(){
let "hoursToGo = hoursToGo + $1/60"
let "minutesToGo = $1%60"
}
convertSeconds(){
let "hoursToGo = hoursToGo + $1/3600"
let "minutesToGo = minutesToGo + ($1%3600)/60"
let "secondsToGo = ($1%3600)%60"
}
help(){
echo "Usage:"
echo "timer *number* hour(s)"
echo "OR"
echo "timer *number* minute(s)"
echo "OR"
echo "timer *number* second(s)"
echo "OR"
echo "timer *number* hour(s) *number* minute(s)"
echo "OR"
echo "timer *number* hour(s) *number* second(s)"
echo "OR"
echo "timer *number* hour(s) *number* minute(s) *number* second(s)"
echo "OR"
echo "timer *number* *unit(second(s), hour(s), minute(s))*"
echo "OR"
echo "timer *number* (this will default to seconds)"
echo "OR"
echo "timer (it will request the values)"
return
}
if [ -z $* ] ; then
read -rp "Hour(s): " "hoursToGo"
read -rp "Minute(s): " "minutesToGo"
read -rp "Second(s): " "secondsToGo"
elif [[ $1 == "h" ]] || [[ $1 == "help" ]] ; then
help
return
elif [ ! -z $1 ] && [ -z $2 ] ; then
convertSeconds $1
elif [[ $2 == *"second" ]] || [[ $2 == *"seconds" ]]; then
convertSeconds $1
elif [[ $2 == *"minute" ]] || [[ $2 == *"minutes" ]]; then
if [ -z $3 ] && [ -z $4 ] ; then
convertMinutes $1
elif [[ $4 == *"second" ]] || [[ $4 == *"seconds" ]]; then
convertMinutes $1
convertSeconds $3
else
help
return
fi
elif [[ $2 == *"hour" ]] || [[ $2 == *"hours" ]]; then
if [ -z $3 ] && [ -z $4 ] ; then
convertHours $1
elif [[ $4 == *"minute" ]] || [[ $4 == *"minutes" ]]; then
if [ -z $5 ] && [ -z $6 ] ; then
convertHours $1
convertMinutes $3
elif [[ $6 == *"second" ]] || [[ $6 == *"seconds" ]]; then
convertHours $1
convertMinutes $3
convertSeconds $5
fi
elif [[ $4 == *"second" ]] || [[ $4 == *"seconds" ]]; then
convertHours $1
convertSeconds $3
fi
else
help
return
fi
let "time=(hoursToGo*3600)+(minutesToGo*60)+secondsToGo"
SECONDS=0
while (( $SECONDS < $time )) ; do
let "hoursPassed=SECONDS/3600"
let "minutesPassed=(SECONDS%3600)/60"
let "secondsPassed=(SECONDS%3600)%60"
echo -ne "$hoursPassed hour(s), $minutesPassed minute(s), $secondsPassed second(s)/$hoursToGo hour(s), $minutesToGo minute(s), $secondsToGo second(s)" \\r
sleep 1
done
echo
echo "Done!"
}