#!/bin/bash
#organization: Seneca College
#Purpose: Validate a date
#Usage: chkdate year month day
#
year=$1; month=$2; day=$3; extra=$4
if [[ "$year" == "" || "$month" == "" || "$day" == "" ]]; then
# Not enough data!
echo Usage: chkdate year month day
exit 0
fi
if [[ ! ( $year =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $day =~ ^[0-9]+$ ) ]]; then
# Date not numeric!
echo Usage: chkdate year month day
exit 1
fi
if [[ $year -lt 1 || $year -gt 9999 || $month -lt 1 || $month -gt 12 || $day -lt 1 || $day -gt 31 ]]; then
# Date out of range!
echo Usage: chkdate year month day
exit 2
fi
if [[ ( $month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12 ) && $day -gt 31 ]]; then
# Invalid day!
echo Usage: chkdate year month day
exit 3
fi
if [[ ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) && $day -gt 30 ]]; then
# Invalid day!
echo Usage: chkdate year month day
exit 4
fi
if [[ ($month == 9) && ($year == 1752) && ( $day -gt 2) && $day -le 14 ]] ; then
#invalid day!
echo Usage: chkdate year month day
exit 5
date -d $2/$3/$1 > /dev/null 2>&1
if [[ "$@" ]] ; then
echo "valid date"
else
echo "not a valid"
fi
它說我的腳本有第 44 行,但我的腳本總行數是 43
答案1
你有一個未終止的if
循環。新增一個fi
for 這個if
循環。
if [[ ($month == 9) && ($year == 1752) && ( $day -gt 2) && $day -le 14 ]]
添加 後fi
,當我運行您的腳本時,這就是我得到的輸出。
chkdate 1342 1 21
valid date
chkdate 13as 1 21
Usage: chkdate year month day