#!/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
shell script习题一则:给你算算还有多久生日
最新推荐文章于 2022-06-05 22:19:05 发布