关闭

Shell Script计算日期

678人阅读 评论(0) 收藏 举报
分类:
#!/bin/bash
#Program: 
#    You input your demobilization date, I calculate how many days before you demobilize
#History:
#    2013/08/26  ydonghao
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#1,告知用户程序的用途,并且告知应该如何输入日期
echo "This program will try to calculateL ;"
echo "How many days before you demobilization date ..."
read -p "Please input your demobilization date (YYYYMMDD ex>20090401):" date2

#2,利用正则表达式测试一下这个输入的内容是否正确 
date_d=$(echo $date2 | grep '[0-9]\{8\}')
if [ "date_d" == "" ]; then
    echo "You input the wrong date format"
    exit 1
fi

#3,开始计算日期
declare -i date_dem=`date --date="date2" + %s`
declare -i date_now=`date +%s`
declare -i date_total=$((date_dem-$date_now))
declare -i date_d=$((date_total/60/60/24))
if [ "$date_total" -lt "0" ] ; then
    echo "You had been demobilization before:" $((-1*$date_d)) " ago"
else
    declare -i date_h=$(($(($date_total-$date_d*60*60*24))/60/60))
    echo "You will demobillize after $date_d days and $date_h hours."
fi

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:69004次
    • 积分:1652
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:54篇
    • 译文:1篇
    • 评论:3条
    最新评论