views:

344

answers:

3

I have a Perl script which will run in a cron job on linux suse. It will take as input a log file that was generated yesterday. The filename of the log contains the date (i.e. log.20100209)

Can I send yesterday's date with the format in the prompt? Should I create an additional script to get the date and execute? If so, how can I do that?

Thanks

perl myscript.pl -f log.20100209

Edit

Thanks for your help

It worked with:

perl myscript.pl -f log.`date --date='yesterday' '+%Y%m%d'`
+1  A: 

You can get yesterday's date like this:

perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'

You can use this when calling your script at the prompt:

perl myscript.pl -f log.`perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'`

But this is unreadable, and I suggest you write a proper script that calculates yesterday's date, and then calls myscript.pl.

Hans W
Also see these: http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlfaq4/How_do_I_find_yesterday's_date.html and http://flux.org.uk/howto/perl/yesterday_date
Dennis Williamson
+7  A: 

GNU date:

date --date='yesterday' '+%Y%m%d'
Ignacio Vazquez-Abrams
+1 I would prefer this to my own Perl below :)
Hans W
Easy and readable, thanks!
Cesar
A: 

You shouldn't need to send yesterday's date as an additional parameter. You can already get it using two other methods:

  1. Perl's built-in time(), localtime(), or gmtime() functions will give you the current date and time, and you can work with that to determine yesterday's date.
  2. It is already included in the name of your log file, so you can parse the file name to get the date into the format you need.

Perl has a lot of modules to work with dates and times, depending exactly what you need to do.

Vanessa MacDougal
Thanks, but i want something simple that allows to keep independent of the log file
Cesar