在unix shell中,如何将昨天的日期转换为变量? [英] In a unix shell, how to get yesterday's date into a variable?

查看:117
本文介绍了在unix shell中,如何将昨天的日期转换为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本,该脚本执行以下操作以将当日的日期存储在变量'dt'中:

I've got a shell script which does the following to store the current day's date in a variable 'dt':

date "+%a %d/%m/%Y" | read dt
echo ${dt}

我如何将昨天日期添加到变量中?

How would i go about getting yesterdays date into a variable?

基本上,我要实现的是使用grep从日志文件中提取昨天的所有行,因为日志中的每一行都包含"Mon 01/02/2010"格式的日期.

Basically what i'm trying to achieve is to use grep to pull all of yesterday's lines from a log file, since each line in the log contains the date in "Mon 01/02/2010" format.

非常感谢

推荐答案

如果可以使用Perl(并且date不具有yesterday之类的漂亮功能),则可以使用:

If you have Perl available (and your date doesn't have nice features like yesterday), you can use:

pax> date
Thu Aug 18 19:29:49 XYZ 2010

pax> dt=$(perl -e 'use POSIX;print strftime "%d/%m/%Y%",localtime time-86400;')

pax> echo $dt
17/08/2010

这篇关于在unix shell中,如何将昨天的日期转换为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆