shell script习题一则:给你算算还有多久生日

#!/bin/bash
#Program
#You input your birthday,I will calculate how many days before your birthday.
#History
#2019/09/20    Dat     First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH

#1.get input
echo "You input your birthday,I will calculate how many days before your birthday."
read -p "Please input your birthday ex> 0101 : " bthd                   #生日

#2.check input
date_b=$(echo $bthd |grep '[0-9]\{4\}')
if [ "$date_b" == "" ];then
        echo "You input the wrong date format..."
        exit 1
fi

#3.calculate
declare -i year_now=`date +%Y`                                          #今夕是何年
declare -i year_next=$(($year_now+1))                                   #明年,今夕是何年+1
declare -i date_bir_now=`date +%s --date="${year_now}${bthd}"`          #今年生日秒数(时间戳)
declare -i date_bir_next=`date +%s --date="${year_next}${bthd}"`        #明年生日秒数
declare -i date_now=`date +%s`                                          #当前秒数
declare -i date_total=$(($date_bir_now-$date_now))                      #剩余秒数=今年生日秒数-现在秒数

if [ "$(($date_total/60/60/24))" -lt "0" ];then                         #剩余天数<0
        date_total=$(($date_bir_next-$date_now))                        #明年生日剩余秒数
        date_how_d=$(($date_total/60/60/24))                            #生日剩余天数
        date_how_h=$(($(($date_total-$date_how_d*60*60*24))/60/60))     #生日剩余小时
        echo "Your next birthday is ${year_next}${bthd}."
        echo "There will be your birthday after $date_how_d days and $date_how_h hours."
elif [ "$(($date_total/60/60/24))" == "0" ];then                        #剩余天数=0
        if [ "$date_total" -le "0" ];then                               #剩余天数=0,秒数<=0,今天生日
                echo -e "\nOH!Today is ${year_now}${bthd},that's your birthday!\n"
                echo -e "       HAPPY BIRTHDAY TO YOU!\n"
        else                                                            #剩余天数=0,秒数>0,明天生日
                echo -e "\nToday is ${year_now}${bthd},tomorrow will be your birthday!\n"
                date_how_d=$(($date_total/60/60/24))                            #生日剩余天数
                date_how_h=$(($(($date_total-$date_how_d*60*60*24))/60/60))     #生日剩余小时
                echo -e "There will be your birthday after $date_how_h hours.\n"
                echo -e "       HAPPY BIRTHDAY!\n"
        fi
else
        date_how_d=$(($date_total/60/60/24))                            #生日剩余天数
        date_how_h=$(($(($date_total-$date_how_d*60*60*24))/60/60))     #生日剩余小时
        echo "Your next birthday is ${year_now}${bthd}."
        echo "There will be your birthday after $date_how_d days and $date_how_h hours."
fi
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值