Tal.Ben

迷茫了太久,想往深了做几件事

Shell Script计算日期

#!/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

阅读更多
版权声明:有关于交易的部分,如果有使用blog的策略,盈亏自负。 https://blog.csdn.net/ydonghao2/article/details/17080901
个人分类: Python 和 Shell Script
上一篇Shell Script中$0等表示的含义
下一篇LINUX 进程控制程序设计
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭