I am trying to validate two time values passed by a program in form of a value called $input. Once validated the time values will be used in a SQL insert statement. I am unable to use the date command if the values are out of range as I get an error message.
A time value must be passed to the database like xx:xx so 08:20 cannot be passed as 8:20 and has to be within the valid range of 00:00 to 23:59. I have split $input and derived two time values $startt and $finisht via awk. $finisht must be larger than $startt.
If the previous criteria is not met I want to open Zenity input boxes with two time fields until the correct criteria has been entered into them.
So far I have the following Bash script but it is not working. Can someone please help?
#!/bin/bash
input=30:20,12:45
startt=$(echo $input | awk -F, -v OFS=, '{print $1}')
finisht=$(echo $input | awk -F, -v OFS=, '{print $2}')
st=date --date="$startt" +%s
ft=date --date="$finisht" +%s
let "tDiff=$ft-$st"
if [[ ! $startt =~ [0-1][0-9]:[0-5][0-9] ]] && [[ ! $startt =~ [0-2][0-3]:[0-5][0-9] ]] || [[ ! $finisht =~ [0-1][0-9]:[0-5][0-9] ]] && [[ ! $finisht =~ [0-2][0-3]:[0-5][0-9] ]] || [[ "$tDiff" -le 0 ]];
then
until [[ $b1 =~ [0-1][0-9]:[0-5][0-9] ]] || [[ ! $b1 =~ [0-2][0-3]:[0-5][0-9] ]] && [[ ! $b2 =~ [0-1][0-9]:[0-5][0-9] ]]
|| [[ $b2 =~ [0-2][0-3]:[0-5][0-9] ]] && [[ "$tzDiff" -le 0 ]]; do
var2="$(zenity --forms --title="start_time and/or finish_time are incorrect" --text "Add a start_time and a finish_time" --separator="," \
--add-entry="WARNING! Something went wrong. Please enter a valid start_time: " \
--add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")"
b1=$(echo $var2 | awk -F, -v OFS=, '{print $1}')
b2=$(echo $var2 | awk -F, -v OFS=, '{print $2}')
tz1=`date --date="$b1" +%s`
tz2=`date --date="$b2" +%s`
let "tzDiff=$tz2-$tz1"
done
fi
echo $var2