【发布时间】:2014-10-06 10:33:18
【问题描述】:
我的脚本从 FTP 下载文件并将文件移动到 FTP 中的存档目录。 我有一个在 FTP 中搜索文件的模式,并将其放入 foreach 循环中以将这些文件放入本地目录。
my $ftpUser = 'xxxx';
my $ftpPW = 'xxxxxx';
my $FTPHost = "xxxxxxxxxxx";
my $remotefile = 'CAP*.csv';
my $archivefile = "/usr/archive/CAP_${file_date_time}.csv";
my $ftp = Net::FTP->new($FTPHost);
$ftp->login( $ftpUser, $ftpPW ) or die print L1 "Could not login FTP :" . $ftp->message . "\n";
print L1 "Login to FTP was successfull\n";
$ftp->cwd("/")
or die print L1 "ftp_cd failed: " . $ftp->message . "\n";
foreach my $file_to_fetch ( $ftp->ls($remotefile) ) {
$ftp->get( $file_to_fetch, $localfile ) or die print L1 "Could not get file from FTP :" . $ftp->message . "\n";
$remotefile = $file_to_fetch;
print "\$file_to_fetch ::: $file_to_fetch\n";
print L1 "File - ${file_to_fetch} Successfully Downloaded from FTP\n";
$ftp->rename( $file_to_fetch, $archivefile )
or die print L1 "Could not move file to archive directory :" . $ftp->message . "\n";
print L1 "File - ${file_to_fetch} Moved to archive directory as CAP_${file_date_time}.csv\n";
}
$ftp->quit;
print L1 "FTP process was successfully completed\n";
if ( -s $localfile ) {
open F1, "$localfile"
or die( print L1 "$localfile cannot be opened for reading \n" );
} else {
die( print L1 "$localfile does not exist \n" );
}
在执行上述代码时,如果我正在搜索的文件不在 FTP 中,但它没有打印“无法从 FTP 获取文件”日志的 die 语句,而是从 FTP 中出来并继续下一组代码打印 L1 "FTP 进程已成功完成\n"。
请帮助我解决这些问题,为什么 die 语句在 foreach 中不起作用,如果它无法从 FTP 获取文件。
【问题讨论】:
标签: perl