1
votes

I wrote a shell script that downloads a document.

cd Desktop/Reports/folder/

wget http://www.mywebpage.com/data/reports/2012_04_02_data.xls

echo "data downloaded."

The tricky part is that the document I want to load (2012_04_02_data.xls) is dated for the previous day. So I need to download the previous days data, not the current day.

My goal is to run the shell script without having to increment the calendar day each morning. Instead, the shell will take the current day and subtract one calendar day and then load that date within the url.

Thanks for the help!

4
Very helpful. Thank you everybody! - user12764

4 Answers

7
votes

How about:

wget "http://www.mywebpage.com/data/reports/$(date -d yesterday +%Y_%m_%d)_data.xls"

This ought to work for GNU coreutils, which based on the linux tag I'm guessing you have.

2
votes

You can use the date command

$ wget "http://www.mywebpage.com/data/reports/$(date -d yesterday +'%Y_%m_%d')_data.xls"
1
votes

The more tricky part is, if you have to handle off days. Usually I wget the link containing page and then grep the date out of the href attribute. then pass the string to your "real" wget command.

1
votes

You tagged this with perl, so here's how I'd do it with Perl. That doesn't mean I think you should use Perl though:

use v5.10.1;

use DateTime;
use Mojo::UserAgent;

chdir 'Desktop/Reports/folder' or die "Could not chdir: $!";

my $date = DateTime->now->subtract( days => 1 )->ymd( '_' );
my $url = sprintf 
    "http://www.mywebpage.com/data/reports/%s_data.xls",
    $date;

my $data = Mojo::UserAgent->new->get( $url )->res->body;

You can do more fancy stuff with the user agent to save chunks to a file as it comes in and many other things you might want to do, but you'll have to fill in those bits yourself.