判断输入的日期是否正确的shell脚本

 更新时间:2016年08月14日 11:04:12   投稿:mdxy-dxy  
这篇文章主要介绍了判断输入的日期是否正确的shell脚本,需要的朋友可以参考下

今儿个讲得是判断输入的日期是否正确,有利用到我们之前03这个例子中的函数
下面是代码

#!/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)首先判断用户输入的参数个数是否正确,接着case $1 in 语句判断月份是否合理。
2)monthnoToName 函数之前出现在我们之前的第03个脚本案例中,用来转换输入的数字日期为字符串。
3) exceedsDaysInMonth 用来判断天数是否超过对应月的最大天数,紧跟着 if [ "$month" = "Feb" -a "$2" -eq "29" ] ; then if ! isLeapYear $3 ; then 用来判断闰年2月的特殊情况
4)总体的感觉是脚本还是很紧凑的,特别是在判断闰年与2月的关系的那段代码,有点意思。

相关文章

  • Linux下的fdisk指令用法场景分析

    Linux下的fdisk指令用法场景分析

    在Linux系统中,对磁盘进行分区是管理和利用磁盘空间的重要任务之一,本篇将以通俗易懂的方式,详细介绍fdisk指令的前世今生、功能、用法和应用场景,帮助读者全面了解fdisk指令,并掌握其在磁盘管理和问题排查方面的应用,需要的朋友可以参考下
    2024-01-01
  • 一些Linux Shell中的权限相关知识总结

    一些Linux Shell中的权限相关知识总结

    这篇文章主要介绍了一些Linux Shell中的权限相关知识总结,使Linux入门学习中的基础知识,需要的朋友可以参考下
    2015-07-07
  • ansible执行shell脚本的方法

    ansible执行shell脚本的方法

    本文主要介绍了ansible执行shell脚本的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • shell进程监控的实现(ps、sleep、kill)

    shell进程监控的实现(ps、sleep、kill)

    本文主要介绍了shell进程监控的实现(ps、sleep、kill),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • linux常用命令之pip命令示例详解

    linux常用命令之pip命令示例详解

    本文介绍了 pip 命令的基本语法、常用选项和参数,以及一些常见的使用示例,通过学习和掌握 pip 命令,你可以方便地管理和使用 Python 包,提高开发效率,感兴趣的朋友跟随小编一起看看吧
    2023-12-12
  • Linux运维常用命令

    Linux运维常用命令

    这篇文章主要介绍了Linux在日常运维过程中需要经常用到的命令,非常的全面,有需要的小伙伴可以参考下
    2016-12-12
  • 浅谈shell脚本中的控制流结构

    浅谈shell脚本中的控制流结构

    今天小编就为大家分享一篇关于浅谈shell脚本中的控制流结构,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • 阿里云云服务器Linux系统更新yum源Shell脚本

    阿里云云服务器Linux系统更新yum源Shell脚本

    这篇文章主要介绍了阿里云云服务器Linux系统更新yum源Shell脚本,阿里云自建了一个包含大多数系统更新的本地yum源,速度快又好用,需要的朋友可以参考下
    2014-09-09
  • bash shell获取当前脚本的绝对路径(pwd/readlink)

    bash shell获取当前脚本的绝对路径(pwd/readlink)

    有时候,我们需要知道当前执行的输出shell脚本的所在绝对路径,本文主要介绍了bash shell获取当前脚本的绝对路径,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Linux忘记root密码进入单用户模式切换运行级别切换用户

    Linux忘记root密码进入单用户模式切换运行级别切换用户

    这篇文章主要介绍了Linux忘记root密码进入单用户模式切换运行级别切换用户,需要的朋友可以参考下
    2019-08-08

最新评论