我有一个用于通过API检索报告的cron脚本,该API每天安排到cron,并检索前一天活动的报告。
my $date = time2str('%Y-%m-%d', ( time - 86400 ));
my %api = (
'example' => {
'example1' => 'example2',
},
'example3' => {
'example4' => 'example5',
},
);
foreach my $example (keys %api) {
my $file_name = $api{$example}{example}."&date=" . $date;
&function( $file_name, $example, $date );
}
sub function {
my ($file_name, $example, $date) = @_;
my $file_name = $example . $date . ".csv";
open (FH, ">$file_name") || print "unable to write $!";
close(FH);
}
但是现在我想从开始日期15-05-2014到结束日期15-07-2014手动运行脚本。怎么做?
答案 0 :(得分:1)
我认为你必须在你想要运行脚本的所有日子里修改$date
变量:
my %api = (
'example' => {
'example1' => 'example2',
},
'example3' => {
'example4' => 'example5',
},
);
use DateTime;
my $date = DateTime->new(
year => 2014,
month => 5,
day => 15,
);
while ( $date->ymd le '2015-07-15' ){
foreach my $example (keys %api) {
my $file_name = $api{$example}{example}."&date=" . $date;
&function( $file_name, $example, $date );
}
$date = $date->add(days => 1);
}
sub function {
my ($file_name, $example, $date) = @_;
my $file_name = $example . $date . ".csv";
open (FH, ">$file_name") || print "unable to write $!";
close(FH);
}