LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-28-2007, 07:34 PM   #1
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Rep: Reputation: 30
Need variable for yesterday's date


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
 
Old 01-28-2007, 07:49 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 01-28-2007, 07:54 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 01-28-2007, 09:08 PM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 01-28-2007, 09:13 PM   #5
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
Thanks that should get me going.

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

Cheers
 
Old 01-28-2007, 09:49 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 01-28-2007, 10:06 PM   #7
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Original Poster
Rep: Reputation: 30
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.
 
Old 01-28-2007, 10:37 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 01-28-2007, 10:50 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Far out, I didn't know about the -d option to date. Wonderful. Good to find a useful new option to a familiar tool.
 
  


Reply



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 08:48 AM
current date - yesterday's date newbie_adm Linux - Newbie 4 09-04-2006 03:56 PM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 06:49 AM
Setting environment variable to current date/time nafan Programming 1 08-23-2004 09:04 PM
Figuring out yesterday's date within a script chrisk5527 Linux - General 1 02-04-2003 04:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration