El script Bash para validar dos valores de tiempo a través de zenity no funciona

El script Bash para validar dos valores de tiempo a través de zenity no funciona

Estoy intentando validar dos valores de tiempo pasados ​​por un programa en forma de un valor llamado $input. Una vez validados, los valores de tiempo se utilizarán en una declaración de inserción SQL. No puedo usar el comando de fecha si los valores están fuera del rango porque aparece un mensaje de error.

Se debe pasar un valor de hora a la base de datos como xx:xx, por lo que las 08:20 no se pueden pasar como 8:20 y deben estar dentro del rango válido de 00:00 a 23:59. He dividido $input y derivado dos valores de tiempo $startt y $finish a través de awk. $finisht debe ser mayor que $startt.

Si no se cumplen los criterios anteriores, quiero abrir cuadros de entrada de Zenity con dos campos de tiempo hasta que se hayan ingresado los criterios correctos.

Hasta ahora tengo el siguiente script Bash pero no funciona. ¿Puede ayudarme alguien, por favor?

#!/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

Respuesta1

Algunos comentarios :

  • Para eliminar algún mensaje de error debes usar 2>/dev/null.

  • Necesita probar el valor de la variable con más frecuencia.

  • cuando haces algunas pruebas complejas ory andnecesitas usar paréntesis, para estar seguro de lo que quieres.

#!/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

información relacionada