LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 01-28-2007, 08:34 PM   #1
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 214
Thanked: 0
Need variable for yesterday's date


[Log in to get rid of this advertisement]
It's in a script that I didnt't write but this one specifies the date added to a backup filename

date=`/bin/date "+%Y%m%d"`

Now I need another variable that instead of today's date does the same thing but shows me yesterday's date.

I also have a Perl script that does show today's date

#!/usr/bin/perl
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$s = sprintf("%02d%02d%02d", $mday, $mon+1, $year - 100);
print $s . "\n";


I can use either, I just need to get yesterday's date, so how would I adjust this?

Thanks
stefaandk is offline     Reply With Quote
Old 01-28-2007, 08:49 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
You can do it nicely with a Perl one-liner:
Code:
#!/bin/bash

today=$(perl -e 'use Date::Format; $d = time(); print time2str("%Y-%m-%d\n", $d);')
yesterday=$(perl -e 'use Date::Format; $d = time() - (3600*24); print time2str("%Y-%m-%d\n", $d);')

echo "today is $today"
echo "yesterday was $yesterday"
Although you need to make sure you have the Date::Format module installed. In Ubuntu (Edgy), Date:Format is provided by the libtimedate-perl package.
matthewg42 is offline     Reply With Quote
Old 01-28-2007, 08:54 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
Oh, one more thing, the format strings for time2str are the same as those described in the strftime manual page.

Personally, I find that there are certain types of feature which are difficult to write in shell scripts and that cause me to switch my language up to a more sophisticated one like Perl. Any sort of detailed date calculations is one of those, as well as things which ae best implemented using associative arrays (hashes in Perl) or which need complex data structures..

Shell is nice, but it's a bit limited.
matthewg42 is offline     Reply With Quote
Old 01-28-2007, 10:08 PM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Thanked: 0
I did a quick google of

perl date

and came up with:

http://www.stonehenge.com/merlyn/UnixReview/col25.html

This should give you what you need. Hope it helps.
wjevans_7d1@yahoo.co is offline     Reply With Quote
Old 01-28-2007, 10:13 PM   #5
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 214
Thanked: 0

Original Poster
Thanks that should get me going.

yum install perl-time* on redhat for the package btw.

Cheers
stefaandk is offline     Reply With Quote
Old 01-28-2007, 10:49 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Location: Fort Worth, TX, USA
Distribution: Fedora Core 4, LFS
Posts: 2,069
Thanked: 0
No need to go with perl. You can accomplish the same thing with the date command itself, and a bit more clearly as well:
Code:
yesterday_variable=`/bin/date -d '1 day ago'`
I'm not on my box to test right now, but I'm sure you could substitute 'yesterday', 'today - 1 day', 'today - 24 hours', etc. for the original date string.

The date man page gives examples of pre- and post-date date strings.
Dark_Helmet is offline     Reply With Quote
Old 01-28-2007, 11:06 PM   #7
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 214
Thanked: 0

Original Poster
That works too and is nice and ez however, I need it to be in this format

29-01-2007

As I'm appending to a filename.

Cheers.
stefaandk is offline     Reply With Quote
Old 01-28-2007, 11:37 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Location: Fort Worth, TX, USA
Distribution: Fedora Core 4, LFS
Posts: 2,069
Thanked: 0
The date command gives that flexibility too. From the date man page, just use the format specifier. The command becomes:
Code:
yesterday_variable=`/bin/date -d '1 day ago' '+%d-%m-%Y'`
Again, since I'm not on my box at home, I can't test that command. You may have to tweak it a little bit if it doesn't work out-of-the-box. See the date man page for help if you need to modify it.
Dark_Helmet is offline     Reply With Quote
Old 01-28-2007, 11:50 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
Far out, I didn't know about the -d option to date. Wonderful. Good to find a useful new option to a familiar tool.
matthewg42 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
date variable user52 Linux - Newbie 2 11-28-2006 09:48 AM
current date - yesterday's date newbie_adm Linux - Newbie 4 09-04-2006 04:56 PM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 07:49 AM
Setting environment variable to current date/time nafan Programming 1 08-23-2004 10:04 PM
Figuring out yesterday's date within a script chrisk5527 Linux - General 1 02-04-2003 05:47 PM


All times are GMT -5. The time now is 10:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration