Hmm. Some interesting comments here, but when I first read the question, "
My first thought was to just use the "time" command. If you call the script using the 'time' command it will then display time values on completion. For example, I have a script I run called "todaysfiles:
The quote before Tea is different than the quote after ready. I added this to my .bashrc sourced it.
Also added a \n after Tea is ready. countdown 10 shows:
Hi fixit7,
I really like your script so I started to toy around a bit with it.
I edited your script a bit until it mostly did what you asked.
I skipped the fractions part, bash only supports integers.
There are several ways though (by using, for instance, the 'bc' command) but I'll leave that up to you for the moment.
I hope I didn't make a mess of it
( btw I used a line from @charles-nix 's example) :
#!/bin/bash
#
# "I am looking to modify my alarm script
# so that it shows a countdown time as the time elapses"
#
# Sound alarm after specifying time and message
# Must input time delay AND message in double quotes !!
#
# ** sleep can also accept intergers ex. sleep 7.63
# Made alias for it type al and time
# 2> /dev/null suppresses messages for amixer AND (c)vlc
soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"
#set back to original volume when leaving this script, even on Ctrl-C
trap "amixer -D pulse sset Master %85" EXIT
trap "exit" INT TERM
#originalVolume=$(amixer -D pulse get Master | grep -m 1 -o -E [[:digit:]]+%)
clear
amixer -D pulse sset Master 100% > /dev/null 2>&1
if [ -f "$soundfile" ];
then
echo "Soundfile is present."
else
echo -e "File $soundfile does NOT exist.\n\nProgram will now exit.\n"
exit
fi
# Below, I cleaned it up a bit by using a "here document"
[ -z "$2" ] && cat<<ERRORMESSAGE && exit 1
Error!! No time value given and/or message specified !!
Alarm Program 2018
alarm.sh [time value in seconds] Message in double Quotes
alarm 5m = 5 minute alarm
alarm 5h = 5 hour alarm
alarm 5d = 5 day alarm
alarm 1.5m = 1 minute 30 seconds alarm
alarm.sh 1m "Tea is ready."
ERRORMESSAGE
# alternative way to do colors
Reset=$( tput sgr0 ) ; Bl=$( tput blink ) ; Gr=$( tput setaf 2 )
echo -e "${Gr}${Bl}TIMER COUNTING DOWN to $1${Reset}"
# countdown display
# below is the part you are insterested in
# it replaces your "sleep" command
#this converts days,hours,months to seconds
case "$1" in
*d ) fact=86400 ;;
*h ) fact=3600 ;;
*m ) fact=60 ;;
* ) fact=1 ;;
esac
(( period = ${1/[a-z]/} * fact ))
#this calculates the endtime based on 'now'
start=$( date +"%s" ) ; (( finish = start + period ))
# this is the counter, based around charles-nix's counter format idea
while (( period >= 0 ))
do
echo -ne "$(date -u --date @${period} +%H:%M:%S)\t\t\r";
(( period = finish - $( date +%s ) )) 2>/dev/null
done
# Fade-in process:
#you used curly brackets but you probably wanted parenthesis
(
for ((volume = 35; volume <= 85; volume += 2)); do
amixer -D pulse sset Master ${volume}% > /dev/null
sleep .5
done
) &
cvlc --play-and-exit "$soundfile" &> /dev/null
echo $2
gxmessage -fg blue -font 'sans 20' -timeout 2 ' TIME IS UP !!'
Well, I was not clear on what you wanted based on the phrasing of your question. I see others have already offered solutions, so I'll not confuse the issue.