Google Answers Logo
View Question
 
Q: Ftp script on Red Hat and SCO Unix ( No Answer,   5 Comments )
Question  
Subject: Ftp script on Red Hat and SCO Unix
Category: Computers > Programming
Asked by: johngl-ga
List Price: $10.00
Posted: 03 Mar 2005 17:20 PST
Expires: 02 Apr 2005 17:20 PST
Question ID: 484343
I have a simple shell script on SCO Unix that connects to a Red Hat 9
box.  All it does is download some files.  After the files are
downloaded I need to move them to another directory something like a
local "mv *.OUT ../sentfiles" command.  There doesn't seem to be an
easy solution in ftp.  I don't want to create cron jobs or anything
like that.  I just want to add something to the shell script that will
do this.  Maybe a quick telnet connect?  I tried doing that myself but
I can't get around the login in a shell script.  Any help would be
great.  Here is the script so far.  Thanks again.

ftp -n <<EOF
open forte
user john XXXXX
cd /data1/forte50/f0000/ED/outbound.test
bin
prompt
mget *.OUT
quit
EOF
Answer  
There is no answer at this time.

Comments  
Subject: Re: Ftp script on Red Hat and SCO Unix
From: amalchandran-ga on 03 Mar 2005 20:58 PST
 
Probably you can write a perl script. Here is one, Check whether it
works for you. It can transfer only one file. Even if I give *.OUT it
gave me only one file. Probably you can add more code to retrieve all
the file names and get them one by one.

The code below is pretty straight forward. In order to do the second
part of your problem, You can create a shell script, which calls the
perl from it, and the use the move (mv) command to move the downloaded
files to the location you want.

I hope this is of some information to you. 
--Code: Perl--

#!/usr/bin/perl -w                                                              
use Net::FTP;                                                                   
                                                                                
my $server='forte';                                                 
my $user="john";                                                               
my $password="XXXXX";                                                         
my $inputdir="/data1/forte50/f0000/ED/";                                        
my $file=$ARGV[0];                                                              
my $success="true";
                                                                                
print "File: ".$file."\n";                                                      
                                                                                
my $ftp=Net::FTP->new($server) or $success="false";                             
$ftp->login($user, $password) or $success="false";                              
$ftp->pasv() or $success="false";                                               
$ftp->binary() or $success="false";                                             
$ftp->cwd($inputdir) or $success='false';
$ftp->get($file) or $sucess="false";                                          
$ftp->quit;                                                                     
if ($success eq "false")                                                        
{
    print "Failed."                                                             
}                                                                               
else                                                                            
{                                                                               
    print "File successfully transferred.\n"                                    
}                                                                               


--Shell Script--

#!/bin/bash
#Save the perl script as ftp.pl 

#For me asterisk did not work well. 
ftp.pl "YOUR_FILE_NAME"

mv "YOUR_FILE_NAME" ../sentfiles
Subject: Re: Ftp script on Red Hat and SCO Unix
From: johngl-ga on 03 Mar 2005 21:39 PST
 
Thank you for your help however that does what my shell script does. 
Mine works just fine.  What I need is a way to move the files to
another directory after I've downloaded them.  Thanks for your time. 
Have a great one.
Subject: Re: Ftp script on Red Hat and SCO Unix
From: amalchandran-ga on 03 Mar 2005 22:06 PST
 
Actually you can move the files that you have downloaded with the perl
script itself. Please add the line

move ($file, "YOUR_OUTPUT_LOCATION");

after the print for file successfully transferred. 
You need to add 

use File::Copy; 

at the top. 
Now I hope you can ftp and transfer the files with one script.
Subject: Re: Ftp script on Red Hat and SCO Unix
From: johngl-ga on 04 Mar 2005 00:35 PST
 
I need to move the files on the remote server.  Not on the local box.
Subject: Re: Ftp script on Red Hat and SCO Unix
From: amalchandran-ga on 04 Mar 2005 01:05 PST
 
Ok, Sorry for the misunderstanding. 

We can just rename the files on the server. (This actually does the
move, from my exp.)

After the file is successfully transferred, and before ftp->quit. 
 we can have a statement like this:
my $outputloc="YOUR_OUTPUT_LOCATION";
my $newfile=$outputloc."/".$file;
my $oldfile=$inputdir."/".$file;
ftp->rename ($oldfile, $newfile);

Or you can have some idea like this:
Since we have ftp ed all the files out, we can delete the files from
the original place. Now using put, we can put it to the new location.
(In case the above one does not work).

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy