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

查看:25
本文介绍了在 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}

我将如何将 yesterday 日期放入变量中?

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天全站免登陆