首页 > 服务器    日期:2020-10-30 / 浏览

一、创建测试文件test.sh

#!/bin/sh
. /etc/profile

# 参数:
# args[0] ,数据日期,日期格式yyyy-MM-dd
# 取30天以前的日期

function get_date_30daysbefore()
{
 sec=`date -d $1 +%s`
 sec_30daysbefore=$((sec - 86400*30))
 days_before=`date -d @$sec_30daysbefore +%F`
 echo $days_before
}

if [ $# == 1 ]; then
 today=$1
 dates_30=`get_date_30daysbefore $1`
else
 today=`date -d -1days '+%Y-%m-%d'`
 dates_30=`date -d -30days '+%Y-%m-%d'`
fi

echo $today
echo $dates_30

二、无参运行

## 不带参数,从当前时间开始算
$ ./test.sh

2016-10-28
2016-09-29

三、传参运行

$ ./test.sh 2015-10-29
2015-10-29
2015-09-29

总结

以上就是利用shell获取指定日期前N天日期的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章