LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2774|回复: 13

如何在shell中取昨天的时间

[复制链接]
发表于 2003-1-23 17:03:15 | 显示全部楼层 |阅读模式
问个比较弱的问题,如果在shell里取昨天的当前时间?
发表于 2003-1-23 17:28:35 | 显示全部楼层
???能说详细点吗?
 楼主| 发表于 2003-1-23 17:46:40 | 显示全部楼层
就是现在是2003-1-23 18:00:00
那我想得到2003-1-22 18:00:00
应该怎么写shell呢?
发表于 2003-1-23 17:58:54 | 显示全部楼层
恕小弟愚昧,请问你的用途和用意?
 楼主| 发表于 2003-1-23 18:05:56 | 显示全部楼层
不是啦,也是别人问我如何实现?
用复杂一点的匹配倒是可以实现
但是我觉得太麻烦
有没有比较简单的方法呢?
所以就把贴子发在这里,看大家有没有什么办法。
发表于 2003-1-24 00:05:07 | 显示全部楼层
时间的格式是固定的,呵呵,这点对你有什么启发么?
发表于 2003-1-24 10:16:10 | 显示全部楼层
#!/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
发表于 2003-1-24 10:24:13 | 显示全部楼层
date命令的结果是与TZ有关的,因而可以改变TZ来得到昨天的时间:
# date
Thu Jan 23 10:27:21 CST 2003
# export TZ=CST+16
# date
Wed Jan 22 10:27:30 CST 2003
 楼主| 发表于 2003-1-24 10:35:31 | 显示全部楼层
谢谢!
我就是想要它尽量简单
3x
发表于 2003-2-4 23:15:32 | 显示全部楼层
以下两种写法都可以:
date --date="1 day ago"
date --date="1 days ago"
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表