He,YuanHui —— 业精于勤荒于嬉,行成于思毁于随

如果你喜欢一个事,又有这样的才干,那就把整个人都投入进去,就要象一把刀直扎下去直到刀柄一样,不要问为什么,也不要管会碰到什么。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

=================================================
本文为HeYuanHui原作

转载必须确保本文完整并完整保留原作者信息及本文原始链接!

NN:      khler
E-mail: khler@163.com
QQ:     23381103
MSN:   pragmac@hotmail.com
=================================================

 

    自动备份都喜欢在夜里0点,备份当天的日志,但是当系统到了夜里0时0分时,日期已经是第二天了,所以必须减1,当然了,日期的减1可没这么简单,什么月初第一天减1不一定是31,还可能是30,或者29,甚至28,前一个月当然也不能简单的减1完事,还得考虑年份...

    Linux里面就有个相当牛x的命令:"1 days ago",嘿嘿

 

ubuntu:

 


hyh@hyh
-Ubuntu:~$ date
2011年 02月 17日 星期四 
10:40:40 CST
hyh@hyh
-Ubuntu:~$ date --date "1 days ago"
2011年 02月 16日 星期三 
10:40:59 CST
hyh@hyh
-Ubuntu:~$

 

 

CenOS:

 



[root@localhost 
~]# date --date "1 days ago"
2011年 02月 16日 星期三 10:43:29 CST
[root@localhost 
~]# dd=$(date --date "1 days ago" +%Y-%m-%d)

[root@localhost 
~]# echo $dd
2011-02-16
[root@localhost 
~]# 

 

 

 

 

看看下面怎么实现就知道有多复杂,上面的命令有多爽了:

 

#!/bin/sh

# ydate: A Bourne shell script that
#
 prints yestarday's date
#
 Output form: Month Day Year
#
 From Focus on Unix: http://unix.about.com

# Set the current month day and year.
month=`date +%m`
day
=`date +%d`
year
=`date +%Y`

# Add 0 to month. This is a
#
 trick to make month an unpadded integer.
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

# Print the month day and year.
echo $month $day $year
exit 0 

 

 

当然还有减一个时区的,也挺有意思:

$#看当前时区
$echo $TZ
CST-8
$#显示当前时间
$date
Mon Apr 2 15:48:36 CST 2002
$#改变当前时区,
TZ=CST+16;export TZ
$#显示当前时间(中间未改变系统时间,但date命令的显示已为昨天)
Mon Apr 1 15:48:33 CST 2002

posted on 2011-02-17 11:07  He,YuanHui  阅读(2546)  评论(0编辑  收藏  举报

Add to Google