so I had the idea of making a bash function that takes in a time in hh:mm:ss (24 hour format) and makes a timer till that time
kinda like a one time alarm while also showing how much time is left
I was thinking of using watch
command to accomplish this
I had some questions on how to do this,
first how do I use bash to know how much is left till a certain time?
then how do I turn it into human readable format to echo to screen?
also how can I do this for times in another day in the past or future?
please help me create this function and I will post the end result here so everyone can use it.
edit: I managed to make this function from the link @terdon posted
timer ()
{
start="$(date +%s -d $1)";
while [ "$start" -ge $(date +%s) ]; do
days="$(($(($(( $start - $(date +%s) )) * 1 )) / 86400))";
time="$(( $start - `date +%s` ))";
if [ $days -gt 0 ]; then
printf '%s day(s) and %s\r' "$days" "$(date -u -d "@$time" +%H:%M:%S)";
else
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)";
fi;
sleep 0.1;
done
}
I ran into a problem, date -d $1 cannot accept a multi word argument even if all arguments are inside quotations. I use ubuntu and I am using bash, this is the code I ran to do testing. as you can see it will ignore what comes after the first word without quotations and with quotations, it gives error.
$ timer 17:25
00:04:37
$ timer sun
1 day(s) and 06:39:31
$ timer sun 17:25
1 day(s) and 06:39:20
$ timer sun 22:25
1 day(s) and 06:39:09
$ timer "sun 22:25"
date: extra operand ‘22:25’
Try 'date --help' for more information.
bash: [: : integer expression expected