I am trying to create a countdown - most likely the hard way. I have it setup like this:
#! /bin/bash
#When to execute command
execute=$(date +"%T" -d "22:30:00")
#Current time
time=$(date +"%T")
#Subtract one from the other.... the below is line 11
math=$(("$execute"-"$time"))
#Repeat until time is 22:30:00
until [ "$math" == "00:00:00" ]; do
echo "Countdown is stopping in $math"
sleep 1
clear
done
The problem is.... It isn’t working. Here’s the output in the terminal:
/path/to/file/test.sh: line 11: 22:30:00-16:39:22: syntax error in expression (error token is “:30:00-16:39:22”)
Countdown is stopping in
Firstly, the error message, what’s wrong?
Secondly, the “Countdown is stopping in” message should have the hours, minutes, and seconds that the countdown will stop in. Why doesn’t it? Keep in mind I am not a professional.
The problem is with the statement
Because
execute
andtime
contain values in the format%H:%M:%S
. But bash's arithmetic expansion can't evaluate time formats.Instead of
%H:%M:%S
format, you can convert times into seconds, do the arithmetic and then print in the desired format.Something like
Thanks, @P.P for the answer. Your method worked at first, but stopped working after a reboot.... Also, it didn't stop the cycle - meaning it went into negative numbers and never executed commands afterwards. This is what I ended up doing: