shell实例之判断生日

这个实例主要涉及到的知识点有

  1. 输入输出语句
  2. date命令的使用
  3. 正则表达式过滤 输入参数
  4. 基本数学运算
  1 read -p "please input your birthday 'mm-dd' : " date1
  2 echo "today is:"
  3 date +%m-%d
  4 date_d=$(echo ${date1} |grep '[0-9]\{2\}-[0-9]\{2\}')
  5 #echo "$date_d
  6 if [ "${date_d}" == "" ]; then
  7   echo "you input the wrong date formate..."
  8   exit 1
  9 fi
 10 date3=$(date +%Y)"-"${date1}
 11 #echo "${date3}"
 12 #start caculate the time
 13 declare -i date_dem=$(date --date="${date3}" +%s)
 14 #echo "${date_dem}"
 15 declare -i date_now=$(date +%s)
 16 #echo "${date_now}"
 17 declare -i date_total_s=$((${date_dem}-${date_now}))
 18 #echo "${date_total_s}"
 19 declare -i date_d=$((${date_total_s}/60/60/24))
 20 #echo "${date_d}"
 21 if [ "${date_total_s}" -lt "0" ]; then
 22   echo "your birthday had been missed before: "$((-1*${date_d}))" ago"
 23 else
 24   declare -i date_h=$(($((${date_total_s}-${date_d}*60*60*24))/60/60))
 25   echo "your birthday  will comming after ${date_d} days and ${date_h} hours    "
 26 fi

比如

 date_d=$(echo ${date1} |grep '[0-9]\{2\}-[0-9]\{2\}')

这里要求date1的输入格式为 “mm-dd” 如12-12,来表示你输入的生日日期,这里要注意的是“{}”要加转义

然后就是基本的运算

 17 declare -i date_total_s=$((${date_dem}-${date_now}))
 18 #echo "${date_total_s}"
 19 declare -i date_d=$((${date_total_s}/60/60/24))
 20 #echo "${date_d}"

这里可以进行连除操作

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值