in reply to Re^17: transfer a file via SFTP
in thread transfer a file via SFTP

This script looks every 5 minutes for a .txt file.
I'd like to die the script if .txt file is not there, otherwise it creates a lot of backup directories.
Can u pls help me to change it?

Replies are listed 'Best First'.
Re^19: transfer a file via SFTP
by ig (Vicar) on Aug 05, 2009 at 18:31 UTC

    You could add

    die unless(-e $file);

    There are many file tests available. You can read about them in -X.

      sorry to disturb u again, but I have 2 problems:

      1.) I don't know howto move setup of the sftp connection outside the loop.

      2.) to die if the *.txt file is not there, I tried to add these lines:
      chdir "/srv" or die "/srv: $!\n"; -f "/srv/*.txt" or die "NO FILE NO TRANSFER !\n";
      but it doesn't work.

      greeings cc

        "it doesn't work" is a bit too vague for me to be able to help you. What happens? Are any messages produced?

        You need to learn some basic programming and debugging skills. For the former I suggest How to Design Programs or, if that doesn't suite you, find something in Where and how to start learning Perl that does or enroll in an introductory programming course. For the latter, you might find the advice in brian's Guide to Solving Any Perl Problem helpful.

        The -f test doesn't do glob matching on the file name. Your test would only succeed if there were a file named, literally, "/srv/*.txt". Try something like the following:

        my $dir = '/srv'; chdir $dir or die "$dir: $!"; my $sftp=Net::SFTP->new($server, %args) or die "could not open connection to $server\n"; my @files = glob('*.txt'); die 'No files to transfer' unless(@files); foreach my $file (@files) { $sftp->put($file, "/HOME/$file") or die; sleep(5); }