Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verify specific files on a UNIX box before proceeding 2

Status
Not open for further replies.

peeon

MIS
Apr 22, 2004
5
US
Wuzzup everyone.
I'm new to perl but learning extremely fast. I'm trying to create a script that will telnet or ftp into a UNIX box and go to a dir and for specific files specific files before proceeding. I want to add this piece into my main script. If the files exist then continue with the rest of the script. If the files don't exist then abort script and display an error to the end user.

This script below will ftp to UNIX and display the .txt files in that dir to the enduser.

#!/usr/bin/perl
use Net::FTP;

my $robin="testenvironment";
my $buck="/mpe/msne/rfwo";

$ftp=Net::FTP->new($robin,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";

$ftp->login("username","password") or $newerr=1;

push @ERRORS, "Can't login to $robint: $!\n" if $newerr;
myerr() if $newerr;

$ftp->cwd($buck) or $newerr=1;
push @ERRORS, "Can't cd $!\n" if $newerr;
myerr() if $newerr;

@files=$ftp->dir($buck."/*txt") or $newerr=1;
push @ERRORS, "Can't get file list $!\n" if $newerr;
myerr() if $newerr;
print "Check for 1 2 3 txt Files\n";
foreach(@files) {
print "$_\n";

}

sub myerr {
print "Error: \n";
print @ERRORS;
}

print "Press the ENTER key to exit ...";
$pause = <STDIN>;

Thanks for your assistance.
 
One way would be to check for the presence of a particular filename in @files as follows:

Code:
$check_for="testfile.txt";
if (grep {/$check_for/} @files) {
	print "found $check_for\n";
        #continue script, do whatever
} else {
	print "not found $check_for\n";
        #end script
}
 
If you'd like to keep the structure of your script as it now you could add the following code.

Where you have:

foreach(@files) {
print "$_\n";

}

You could change it to read:

foreach(@files) {
unless(/file_I_need/){
# display error to user
}
}


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top