ftp perl script problem..

J

jac

There's this script that suppose to tranfer files..
Weird this is when executed, sometimes its transfering, sometimes its
not.

Could someone help me please? have a look at the script and point me
where the wrong part is? thanks plenty!

#!/usr/bin/perl
use Net::FTP;
require "find.pl";
$hostname = "10.1.1.1"; # Hostname or IP address
$username = "someon"; # Ftp user name
$password = "xxxxxxx"; # Ftp password
$LocalBase = "/path/to/local/files/to/transfer"; #
location of the log directories
$RemotePath = "/path/to/destination"; # where to put archive file on
ftp server
$ArchFile = "log.tar"; # name of archive file
$TAR="/usr/sbin/tar";
$GZIP="/appl/util/gzip";

&find($LocalBase);

if (@FileList > 0)
{ &TransferFiles; }
else
{ $ErrorMsg = "There were not any files to transport at this time"; }



##########################
# TransferFiles()
##########################
sub TransferFiles
{
local ($File, $Goodxfr, $Cnt);
# Initialize variables to zero
$Goodxfr = 0;
$Cnt = 0;
# Change to the Base directory where the log files are written to #
chdir($LocalBase);


##########################
# FtpFiles()
##########################
sub FtpFiles
{
$ftp = Net::FTP->new($hostname, Debug => 0);

$ftp->login($username, $password) ||
&FtpErr ("ftp->login: Please check username and password\n");
$ftp->binary;


$ftp->cwd($RemotePath) ||
&FtpErr("ftp->cwd: Not able to change to $RemotePath\n");

$ftp->put("$DestLocalBase/$ArchFile.gz")||
&FtpErr("ftp->put: Failed transfering
$DestLocalBase/$ArchFile.gz\n");




##########################
# wanted()
##########################
sub wanted
{ # True if file exists and is graeter than the time period
if ( -f $name && -M _ > $TimePeriod )
{
$Name = substr($name,$BaseStrip,); # strips off the local base
directory
push(@FileList,$Name);
# $FileSize{$Name} = (stat($name))[7];
}
}
 
M

Mumia W.

jac said:
There's this script that suppose to tranfer files..
Weird this is when executed, sometimes its transfering, sometimes its
not.

Could someone help me please? have a look at the script and point me
where the wrong part is? thanks plenty!

#!/usr/bin/perl
[...]
##########################
# wanted()
##########################
sub wanted
{ # True if file exists and is graeter than the time period
if ( -f $name && -M _ > $TimePeriod )

You say "-M _" above. Did you mean "-M $_" or "-M $name"?

Although they are probably not needed, I might use extra parenthesis for
the above code:
if ( -f $name && (-M $name > $TimePeriod) )

{
$Name = substr($name,$BaseStrip,); # strips off the local base
directory

Inside the "wanted" function, $_ already has this value.
perldoc File::Find
 
B

Brian Raven

Mumia W. said:
You say "-M _" above. Did you mean "-M $_" or "-M $name"?

In this context, the underscore represents a special filehandle used to save a
system call. This is described in more detail in 'perldoc -f -X'.

HTH

--
Brian Raven
: I've tried (in vi) "g/[a-z]\n[a-z]/s//_/"...but that doesn't
: cut it. Any ideas? (I take it that it may be a two-pass sort of solution).
In the first pass, install perl. :)
-- Larry Wall <[email protected]>
 
T

Tad McClellan

jac said:
Weird this is when executed, sometimes its transfering, sometimes its
not.

Could someone help me please?


You should ask your machine for help *before* resorting to asking
hundreds of people around the world for help.

Perl can help you find many common bugs, but it will only do so
if you ask it to:

use warnings;
use strict;

require "find.pl";


What is in find.pl?

&find($LocalBase);


perldoc perlsub

then you'll probably see that you want

find($LocalBase);

instead.


{ &TransferFiles; }

{ TransferFiles(); }

##########################
# TransferFiles()
##########################
sub TransferFiles
{
local ($File, $Goodxfr, $Cnt);


Why are you using package variables rather than lexical variables?

# Initialize variables to zero
$Goodxfr = 0;
$Cnt = 0;
# Change to the Base directory where the log files are written to #
chdir($LocalBase);


Errr, where is the closing curly brace for the subroutine?

Please post Real Perl Code.


You should check to see if you actually got what you asked for:

chdir($LocalBase) or die "could not cd to '$LocalBase' $!";

# $FileSize{$Name} = (stat($name))[7];


# $FileSize{$Name} = -s $name;
 
D

DJ Stunks

Brian said:
In this context, the underscore represents a special filehandle used to save a
system call. This is described in more detail in 'perldoc -f -X'.

but it only works once you call stat on a file which I don't see the OP
doing, right?

-jp
 
B

Ben Morrow

Quoth "DJ Stunks said:
but it only works once you call stat on a file which I don't see the OP
doing, right?

The -X filetests (except for -t) call stat, so they also populate the _
cache. -l is different from the others in that it calls lstat rather
than stat (for obvious reasons), which values are then put in the cache
instead.

Ben
 
M

Mumia W.

DJ said:
but it only works once you call stat on a file which I don't see the OP
doing, right?

-jp

I read "perldoc -f -X," and now I think that -f *is* a stat operation.
 
U

Uri Guttman

MW> I read "perldoc -f -X," and now I think that -f *is* a stat operation.

almost all of the -X calls are just simple op wrappers around stat. you
can do any of them by calling stat and decoding the inode info you get
back (and there are OO wrappers for that if you want).

the special _ is not a handle but a special marker that perl should use
the data in the stat buffer (which holds the results from the most
recent stat call, wrapped or not) instead of making another stat
call. so if you do more than one stat or -X op on a file or handle, then
using _ will save you stat calls (which are full system calls) for all
but the first one. it is a minor and very specific optimization and only
needed if you go stat crazy. i have rarely used it but i don't seem to
do multiple stat calls on a file.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top