unixdatetimeshellkshhp-ux

In a unix shell, how to get yesterday's date into a variable?


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?

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.

Thanks a lot


Solution

  • 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