
我正在嘗試以名為 $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
.您需要更頻繁地測試變數的值。
or
當你用and 做一些複雜的測試時,and
你需要使用括號,以確定你想要什麼。
#!/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