linux脚本 昨天,linux centos shell 脚本 获取 昨天前天大前天日期

原文:http://www.weiruoyu.cn/?p=481

昨天 dname1="$(date -d -1day +%Y%m%d)"

前天 dname1="$(date -d -2day +%Y%m%d)"

大前天 dname2="$(date -d -3day +%Y%m%d)"

明天 dname1="$(date -d 1day +%Y%m%d)"

后天 dname1="$(date -d 2day +%Y%m%d)"

大后天 dname1="$(date -d 3day +%Y%m%d)"

参考:http://hi.baidu.com/sjh_forever/blog/item/b716c9259ddf1b37d4074281.html

想深入研究可以参考下面的文章:

有几种写法:

1. 使用date命令(最为推荐,但可能在其他平台不适用)。

昨天:yesdate=`date -d yesterday +%Y%m%d`

前天:yesdate1=`date -d -2day +%Y%m%d`

大前天:yesdate2=`date -d -3day +%Y%m%d`

大大前天:yesdate3=`date -d -4day +%Y%m%d`

2. 更改时区方法。

昨天: setenv TZ GMT+16

date +%Y%m%d

前天: setenv TZ GMT+40

date +%Y%m%d

大前天:setenv TZ GMT+64

date +%Y%m%d

然后再把环境变量改回来

setenv TZ PRC

3. 自己写shell判断(最为底层的,不推荐使用,但强烈推荐学习)

month=`date +%m`

day=`date +%d`

year=`date +%Y`

month=`date +%m`

yesdate=""

month=`expr $month + 0`

# Subtract one from the current day.

day=`expr $day - 1` # If the day is 0 then determine the last # day of the previous month.

if [ $day -eq 0 ]; then

# Find the preivous month.

month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of

# the previous year.

if [ $month -eq 0 ]; then

month=12

day=31

year=`expr $year - 1`

# If the month is not zero we need to find

# the last day of the month.

else

case $month in

1|3|5|7|8|10|12) day=31;;

4|6|9|11) day=30;;

2)

if [ `expr $year % 4` -eq 0 ]; then

if [ `expr $year % 400` -eq 0 ]; then

day=29

elif [ `expr $year % 100` -eq 0 ]; then

day=28

else

day=29

fi

else

day=28

fi

;;

esac

fi

fi

case $month in

1|2|3|4|5|6|7|8|9) month="0$month"

esac

case $day in

1|2|3|4|5|6|7|8|9) day="0$day"

esac

# Print the month day and year.

yesdate="$year$month$day"

来源:http://www.weiruoyu.cn/?p=481

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值