how to find out yesterday's date in mmmddyy format in UNIX/PERL ?

what command/script is used to find out yesterday's date in UNIX/PERL ?
rvsBhanuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
print strftime("%m%d%Y", localtime(time - 3600*24));

0
aschueteCommented:
Below is part of a code I wrote before, I believe it will work as a unix shell script.
#! /usr/bin/ksh
# ydate.sh
 
# Today is
T=`date +'%m-%d-%Y'`
# Set month year & date
M=`echo $T | cut -d'-' -f1`
D=`echo $T | cut -d'-' -f2`
Y=`echo $T | cut -d'-' -f3`
# Test for first day of the year first
# Test if first of March
   # Test if leap year
# Test if previous month had 31/30 day
if test $D -eq 1
then
   if test $M -eq 1
     then
        Y=`expr $Y - 1`
   M=12
   D=31
   echo $M/$D/$Y
   exit
   fi
   if test $M -eq 3
     then
        M=`expr $M - 1`
        cal $M $Y | tail +2 | grep -q 29
   if test $? -eq 0
   then
      DAY=29
   else
      DAY=28
        fi
     else
        M=`expr $M - 1`
        cal $M $Y | tail +2 | grep -q 31
   if test $? -eq 0
   then
     D=31
        else
     D=30
        fi
     fi
else
    D=`expr $D - 1`
fi
echo "$M/$D/$Y"

Open in new window

0
mrjoltcolaCommented:
My solution is Perl, by the way. I did not note that, but I see the question is multi-zone now.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Adam314Commented:

use Date::Manip;
 
my $date = UnixDate("Yesterday",'%b%d%y');
print "date=$date\n";
 
 
 
#Or, if you don't want to use Date::Manip, you can do this
#This will could be incorrect around daylight savings time though
my $date = strftime("%b%d%Y", localtime(time - 3600*24));
print "date=$date\n";

Open in new window

0
rvsBhanuAuthor Commented:
well my script looks like this,but it gives the error as I do not have GNU date :

---------------------------------------------------------------------
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date -d yesterday +"%b%d%y")
./script2 $ydate
echo "script2 completed"
#

----------------------------------------------------------------------
here the script1 completes successfully,but I get error while script2 is executing.
error:
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
enter date suffix for the filename in the format mmmyy..
---------------------------------------------------------------------
can you please tell me if the above resolution be incorporated in this ?And if so,how ? Also I would like yesterday's date to be displayed as mmmddyy where mmm is the first 3 letters of the name of the month.
Please suggest ?
0
Adam314Commented:
If you don't care about DST:
ydate=`perl -MPOSIX -e 'print strftime("%b%d%y", localtime(time-86400))'`

Open in new window

0
Adam314Commented:
If you care about DST
perl -MDate::print UnixDate("Yesterday", "%b%d%y")'

Open in new window

0
rvsBhanuAuthor Commented:
So would this work for shell scripting also(the one I have written),or PERL ?

Also what changes would then be required at the time of DST ?
0
rvsBhanuAuthor Commented:
or if you can modify my script to be in PERL so that I can incorporate this..?would be great...
0
Adam314Commented:
The code I gave in post 24863395 is shell script code.  It uses perl to generate the string.  The command I gave in post 24863441 is a perl command.  It would have to be enclosed in backticks to be have the output go to a variable in a shell script.

Using (time-86400) subtracts 1 normal day (24 hours of 60 minutes each with 60 seconds per each minute) from the current time.  The problem with this is that for daylight savings time, a day could be 23 hours or 25 hours.  Using this code within that 1 hour window on those days would give the wrong results.
Using the Date::Manip and "yesterday" does not have this problem.

Here is your code updated to get the yesterday from perl.
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=`perl -MDate::print UnixDate("Yesterday", "%b%d%y")'`
./script2 $ydate
echo "script2 completed"

Open in new window

0
rvsBhanuAuthor Commented:
when I run the below script,it gives me the o/p as it is :
script :
-----------------------------------------------------------
#!/bin/ksh
ydate='perl -MDate::print UnixDate("Yesterday","%b%d%y")'
echo $ydate
#
-----------------------------------
o/p:
perl -MDate::print UnixDate("Yesterday","%b%d%y")
-------------------------------------------------------------------
can you figure out what can be the issue here ?
0
ozoCommented:
ydate='perl -MDate::print UnixDate("Yesterday","%b%d%y")'
should be
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")'`

note `` not ''


0
rvsBhanuAuthor Commented:
Is there any sp. feature that is required to be installed on the UNIX box to be able to use manip command or is it inbuilt ?
0
ozoCommented:
cpan Date::Manip
0
Adam314Commented:
You will need to have the Date::Manip module installed.  The command ozo gave:
    cpan Date::Manip
will install this module.  If it isn't installed, and you don't want to install it, you can use the other command I gave.  It will work the same, with the exception of the DST problem I mentioned.
0
ozoCommented:
To handle DST without Date::Manip, you could do something like

ydate=`perl -MPOSIX -e 'print strftime"%b%d%y", localtime time-3600*(12+(localtime)[2])'`
0
rvsBhanuAuthor Commented:
this is my current script:
-------------------------------------------------------------------------------
#!/bin/ksh
ydate='perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")''
echo $ydate
------------------------------------------------------------------------------
error:
 syntax error at line 2 : `(' unexpected
--------------------------------------------------------------------------------
Can you pls chk this and let me know what needs to be done for this ?
0
rvsBhanuAuthor Commented:
the other one also threw an error.Here is the script :
#!/bin/ksh
ydate='perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[2])''
echo $ydate
--------------------------------------------------------------
error:
strftime%b%d%y,localtime:  not found
----------------------------------------------------------------
Can someone determine the issue here and let me know if there is any syntax problem somewhere ?

Also is there a way to determine if manip is there on the box or not ?
0
Adam314Commented:
You are using forward quote ' character, when you need to be using backtick ` character.  The backtick is the key to the left of the "1" on a keyboard.


ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")'`
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y", localtime time-3600*(12+(localtime)[2])'`

Open in new window

0
rvsBhanuAuthor Commented:
Script:
------------------------------------------
#!/bin/ksh
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")'`
echo $ydate
-----------------------------------------------------------------------------------------------------------------
Error:
Can't locate Date/Manip.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-solaris-64int /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /usr/perl5/vendor_perl/5.6.1 /usr/perl5/vendor_perl .).
BEGIN failed--compilation aborted.
----------------------------------------------------------------
However,
#!/bin/ksh
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[2])'`
echo $ydate
--------------------------------------------------------------
This works fine.But jus wanted to confirm that would this be taking the DST into account or there is something else that I need to do ?
0
Adam314Commented:
The first error is because you do not have Date::Manip installed.  You could install it with (run as root):
    cpan Date::Manip

If the second is working, you can use that.  It will take DST into account.
0
rvsBhanuAuthor Commented:
cool...thanks...
jus a small clarification...will the second script take the 1st of the month,leap years and DST into account ?
ie.ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[2])'`

I mean is there any specific instance where it fails ?though I hope it doesn't...
0
Adam314Commented:
Yes, it'll handle all of those.  It should be correct in all cases.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.