let we have 3 time
time1=11:34:45
time2:12:39:32
target_time:12:45:48
so how to find that the target time is greater or equal to time1 or time2?
required output:
the target time 12:45:48 is greater or equal to 12:39:32
You can begin by converting the time to a common format that would be much easier to compare, like in seconds.
This can be done like so in a bash function:
time_to_seconds() {
IFS=: read -r hours minutes seconds <<< "$1"
echo $(( hours * 3600 + minutes * 60 + seconds ))
}
IFS=: tells bash to separate strings by colons, so that the hours, minutes, and seconds can be read using read.
After that, you can convert your time variables to seconds like this:
time1_secs=$(time_to_seconds "$time1")
time2_secs=$(time_to_seconds "$time2")
target_time_secs=$(time_to_seconds "$target_time")
Then it's just a matter of doing comparisons, like this:
if [ $target_time_secs -ge $time2_secs ]; then
echo "the target time $target_time is greater or equal to $time2"
fi
time1andtime2etc. tag).