通过 zenity 验证两个时间值的 Bash 脚本不起作用

通过 zenity 验证两个时间值的 Bash 脚本不起作用

我正在尝试验证程序以名为 $input 的值的形式传递的两个时间值。验证后,时间值将用于 SQL 插入语句。如果值超出范围,我将无法使用 date 命令,因为我会收到错误消息。

必须将时间值(如 xx:xx)传递给数据库,因此 08:20 不能作为 8:20 传递,并且必须在 00:00 到 23:59 的有效范围内。我已拆分 $input 并通过 awk 得出两个时间值 $startt 和 $finisht。$finisht 必须大于 $startt。

如果不满足先前的标准,我希望打开带有两个时间字段的 Zenity 输入框,直到在其中输入正确的标准。

到目前为止,我有以下 Bash 脚本,但它不起作用。有人可以帮忙吗?

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

答案1

一些评论:

  • 要删除某些错误消息,您应该使用2>/dev/null

  • 您需要更频繁地测试变量的值。

  • 当您进行一些复杂的测试时orand您需要使用括号来确定您想要什么。

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

相关内容