0

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

Christian Hick
  • 331
  • 4
  • 12

1 Answers1

1

Some comments :

  • To remove some error message you should use 2>/dev/null .

  • You need to test value of variable more often .

  • when you do some complex tests with or and and you need to use paranthesis , to be sure of what you want .

#!/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}')

#
#  when you call date you need to redirect error messages to /dev/null 
#

st=$( date --date="$startt" +%s  2>/dev/null )
ft=$( date --date="$finisht" +%s 2>/dev/null ) 

#
# i will compute the diff only if i have 2 values   
#

if [ -n "$st" -a "$ft" ] ; then
    #
    # because i had 2 numbers  , conversion to timestamp was good  
    # i will normalize value of startt / finisht
    #
    startt=$(date +%H:%M  -d "$startt"  )
    finisht=$(date +%H:%M -d "$finisht" )

    #
    # we recompute the timestamp to be sure that the normalization does not change
    #
    st=$( date --date="$startt" +%s  2>/dev/null )
    ft=$( date --date="$finisht" +%s 2>/dev/null ) 

    tzdiff=$(( ft - st ))
else
    tzdiff=0
fi    

#
#   
#   test for starttt must be enclosed with ( )
#   test for finisht must be enclosed with ( )
#

while [[  ( ( ! "$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]$ ) ) || 
          ( "$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: ")"
    #
    # we set tzdiff to zero so we are going to loop for ever if nothing the value 
    #
    tzdiff=0
    #
    # if var2 is empty do nothing 
    #
    if [ -n "$var2" ] ; then
       b1=$(echo "$var2" | cut -d, -f1 )
       b2=$(echo "$var2" | cut -d, -f2 )
       #
       # if b1 or b2 is empty do nothing 
       #
       if [ -n "$b1" -a -n "$b2"  ] ; then
           tz1=$( date --date="$b1" +%s 2>/dev/null )
           tz2=$( date --date="$b2" +%s 2>/dev/null )
           #
           # if tz1 or tz2 is empty do nothing 
           #
           if [ -n "$tz1" -a -n "$tz2" ] ; then
              startt=$(date +%H:%M -d $b1 )
              finisht=$(date +%H:%M -d $b2 )

              tz1=$( date --date="$b1" +%s 2>/dev/null )
              tz2=$( date --date="$b2" +%s 2>/dev/null )

              tzdiff=$(( tz2 - tz1 ))
           fi
       fi
    fi
    echo $var2 $startt $finisht $tzdiff
done

echo $var2 $startt $finisht $tzdiff
EchoMike444
  • 501
  • 1
  • 4
  • 5