判斷輸入的日期是否正確的shell腳本
今兒個(gè)講得是判斷輸入的日期是否正確,有利用到我們之前03這個(gè)例子中的函數(shù)
下面是代碼
#!/bin/sh # valid-date -- Validates a date, taking into account leap year rules. exceedsDaysInMonth() { case $(echo $1|tr '[:upper:]' '[:lower:]') in jan* ) days=31 ;; feb* ) days=28 ;; mar* ) days=31 ;; apr* ) days=30 ;; may* ) days=31 ;; jun* ) days=30 ;; jul* ) days=31 ;; aug* ) days=31 ;; sep* ) days=30 ;; oct* ) days=31 ;; nov* ) days=30 ;; dec* ) days=31 ;; * ) echo "$0: Unknown month name $1" >&2; exit 1 esac if [ $2 -lt 1 -o $2 -gt $days ] ; then return 1 else return 0 # the day number is valid fi } isLeapYear() { year=$1 if [ "$((year % 4))" -ne 0 ] ; then return 1 # nope, not a leap year elif [ "$((year % 400))" -eq 0 ] ; then return 0 # yes, it's a leap year elif [ "$((year % 100))" -eq 0 ] ; then return 1 else return 0 fi } ## Begin main script if [ $# -ne 3 ] ; then echo "Usage: $0 month day year" >&2 echo "Typical input formats are 8 3 2002" >&2 exit 1 fi # Normalize date and split back out returned values if [ $? -eq 1 ] ; then exit 1 # error condition already reported by normdate fi monthnoToName() { # Sets the variable 'month' to the appropriate value case $1 in 01|1 ) monthd="Jan" ;; 02|2 ) monthd="Feb" ;; 03|3 ) monthd="Mar" ;; 04|4 ) monthd="Apr" ;; 05|5 ) monthd="May" ;; 06|6 ) monthd="Jun" ;; 07|7 ) monthd="Jul" ;; 08|8 ) monthd="Aug" ;; 09|9 ) monthd="Sep" ;; 10) monthd="Oct" ;; 11) monthd="Nov" ;; 12) monthd="Dec" ;; * ) echo "$0: Unknown numeric month value $1" >&2; exit 1 esac return 0 } monthnoToName $1 month="$monthd" day="$2" year="$3" if ! exceedsDaysInMonth $month "$2" ; then if [ "$month" = "Feb" -a "$2" -eq "29" ] ; then if ! isLeapYear $3 ; then echo "$0: $3 is not a leap year, so Feb doesn't have 29 days" >&2 exit 1 fi else echo "$0: bad day value: $month doesn't have $2 days" >&2 exit 1 fi fi echo "Valid date: $newdate" exit 0
分析:
1)首先判斷用戶輸入的參數(shù)個(gè)數(shù)是否正確,接著case $1 in 語句判斷月份是否合理。
2)monthnoToName 函數(shù)之前出現(xiàn)在我們之前的第03個(gè)腳本案例中,用來轉(zhuǎn)換輸入的數(shù)字日期為字符串。
3) exceedsDaysInMonth 用來判斷天數(shù)是否超過對(duì)應(yīng)月的最大天數(shù),緊跟著 if [ "$month" = "Feb" -a "$2" -eq "29" ] ; then if ! isLeapYear $3 ; then 用來判斷閏年2月的特殊情況
4)總體的感覺是腳本還是很緊湊的,特別是在判斷閏年與2月的關(guān)系的那段代碼,有點(diǎn)意思。
相關(guān)文章
Linux監(jiān)控cpu以及內(nèi)存使用情況之top命令(詳解)
下面小編就為大家?guī)硪黄狶inux監(jiān)控cpu以及內(nèi)存使用情況之top命令(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05shell腳本實(shí)現(xiàn)公司項(xiàng)目部署交付環(huán)境預(yù)檢查的思路
這篇文章主要介紹了shell腳本實(shí)現(xiàn)公司項(xiàng)目部署交付環(huán)境預(yù)檢查的思路,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09linux 環(huán)境 mysql寫入中文報(bào)錯(cuò)
本篇文章主要介紹了linux 環(huán)境 mysql寫入中文報(bào)錯(cuò)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Linux 使用shell腳本處理字符串的實(shí)現(xiàn)
這篇文章主要介紹了Linux 使用shell腳本處理字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Shell腳本實(shí)現(xiàn)批量生成nagios配置文件
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)批量生成nagios配置文件,本文使用配置文件模板加上文件替換的方法實(shí)現(xiàn)批量生成配置文件,需要的朋友可以參考下2015-07-07