tags:

views:

68

answers:

1

I have a script that has been running for over a year and now it is failing:

It is creating a command file:

open ( FTPFILE, ">get_list");
print FTPFILE "dir *.txt"\n";
print FTPFILE "quit\n";
close FTPFILE;

Then I run the system command:

$command = "ftp ".$Server." < get_list | grep \"\^-\" >new_list";
$code = system($command);

The logic the checks:

if ($code == 0) {

do stuff
} else {
log error
}

It is logging an error. When I print the $code variable, I am getting 256.

I used this command to parse the $? variable:

$exit_value  = $? >> 8;
$signal_num  = $? & 127;
$dumped_core = $? & 128;

print "Exit: $exit_value Sig: $signal_num Core: $dumped_core\n";

Results:

Exit: 1 Sig: 0 Core: 0

Thanks for any help/insight.

+5  A: 

Mel - you might gain a bit more information by looking at standard error output of the ftp command.

1) Does the FTP command work by hand from shell prompt?

2) If command line ftp works, capture the output (stdout and stderr) of the ftp command and print it in Perl script. For a couple of ways to do so, see perlfaq8 - How can I capture STDERR from an external command?

The two easiest apporaches are these:

my $output = `$command 2>&1`; 

my $pid = open(PH, "$command 2>&1 |"); 
while (<PH>) { print "Next line from FTP output: $_"; } 

3) As wisely noted by Snake Plissken in a comment, an alternate (and more idiomatic and possibly easier) approach is to scrap the system call to "ftp" command and instead use Net::FTP Perl module.

DVK
+1 for Net::FTP...I inherited a godawful pile of scripts that did this bizarre command-line mojo with ncftp, and the resulting nightmare has cured me from ever using anything except Net::FTP.
Satanicpuppy
@Satanicpuppy - find a random **good** answer from Snake Plissken and upvote him - his comment was posted before my answer ;)
DVK