免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9363 | 回复: 13
打印 上一主题 下一主题

shell 如何得到前一天的日期 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-12 10:01 |只看该作者 |正序浏览
网上虽然有很多这种方法.

但都不符合我的要求.

我需要的是在unix下运行的shell, 格式需要是 yyyymmdd

如 20100112 的前一天是 20100111 ..

论坛徽章:
0
14 [报告]
发表于 2010-01-13 14:40 |只看该作者
dati=`perl -e '($a,$b,$c,$day,$mon,$year,$d,$e,$f) = localtime(time-86400);printf "%4d%02d%02d",$year+1900,$mon+1,$day'`

论坛徽章:
0
13 [报告]
发表于 2010-01-13 13:51 |只看该作者
谢谢!!!

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
12 [报告]
发表于 2010-01-12 14:58 |只看该作者

回复 #11 wazhl 的帖子

参考:
http://archives.devshed.com/foru ... -y-m-d-1795682.html

as long as you use the format TZ=<string><offset>

<stringwill just be some descriptive text that you chose to
describe that rule. As long as you specify an offset (it could
be even more complicated like having two offsets for day light
saving time, and rules to tell when to switch to DST), then the
<stringis not used to define the rules (aaa will not refer to
any description file in /usr/share/zoneinfo contrary to another
contributor said), it will just be used in date +%Z.

So, TZ=aaa24 is a tab zone called "aaa" that is defined as being
offset by 24 wrt Universal time (GMT, UTC).

If you had specified:

TZ=aaa

so, without any rule embeded in $TZ, then "aaa" would have
refered to a system wide timezone, whose rules are defined in a
system wide description file (on some systems in
/usr/share/zoneinfo).

$ TZ=aaa truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT

My system doesn't have such system-wide timezone.

$ TZ=Brazil/Acre truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
open("/", RDNLY) = 3
Wed Feb 15 09:24:47 AST 2006

It does have one called Brazil/Acre, but if I decide to specify
the rules for Brazil/Acre to say that it's 24 hours behind UTC:

$ TZ=Brazil/Acre24 truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
Tue Feb 14 14:25:41 Brazil/Acre 2006

the system-wide description file is not used.

论坛徽章:
0
11 [报告]
发表于 2010-01-12 14:21 |只看该作者
原帖由 Shell_HAT 于 2010-1-12 14:17 发表

$ date
Tue Jan 12 14:18:00 EAT 2010

$ YESTERDAY=`TZ=aaa24 date +%Y%m%d`

$ echo $YESTERDAY
20100111

$ uname
HP-UX


响...这样不会影响系统的其他进程吧?这个方法看起来很不错哎.

TZ=aaa24都是相差24小时的意思吗?

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
10 [报告]
发表于 2010-01-12 14:17 |只看该作者

$ date
Tue Jan 12 14:18:00 EAT 2010

$ YESTERDAY=`TZ=aaa24 date +%Y%m%d`

$ echo $YESTERDAY
20100111

$ uname
HP-UX

论坛徽章:
0
9 [报告]
发表于 2010-01-12 14:09 |只看该作者
An example here:

$ echo $TZ
UTC0
$ date
Tue Jan 12 06:07:16 UTC 2010
$ TZ=UTC24
$ date
Mon Jan 11 06:07:33 UTC 2010


Do you get the point?

论坛徽章:
0
8 [报告]
发表于 2010-01-12 13:58 |只看该作者
原帖由 nhw_cs 于 2010-1-12 12:21 发表



set TZ envroment variable



如何在shell里实现呢?

论坛徽章:
0
7 [报告]
发表于 2010-01-12 12:21 |只看该作者
原帖由 wazhl 于 2010-1-12 11:36 发表
hp_ux  

不支持 date "+%Y%m%d" -d "yesterday" 这些格式的.



set TZ envroment variable

论坛徽章:
0
6 [报告]
发表于 2010-01-12 11:36 |只看该作者
hp_ux  

不支持 date "+%Y%m%d" -d "yesterday" 这些格式的.
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP