shell计算距离生日的天数

2016-11-16

利用时间戳计算秒差,除以天的秒数86400.

1、例如birthday.sh

#!/bin/sh

if [[ -z $1 ]];then
echo Usage $0 20170101;
exit 0;
fi
birthday=$1
startDate=`date -d now +%s`
endDate=`date -d $birthday +%s`

if [[ $startDate -gt $endDate ]];then
echo today is:$(date +%Y%m%d)
echo Please input date after today.
exit 0;
fi

((stampDiff=endDate-startDate))
((dayDiff=stampDiff/86400))
echo Your Birthday is:$(date -d $birthday +%Y%m%d)
echo There is still $dayDiff days from your birthday;

for ((i=2;i<=$dayDiff;i=i+2));
do
((sum=sum+i))
done

echo $sum

2、实例:

./birthday 20161129

Your Birthday is:20161129
There is still 12 days from your birthday

分类:Linux | 标签: |

相关日志

评论被关闭!