Solved

How to find the status of FTP operation inside shell script

Posted on 2006-06-27
3
513 Views
Last Modified: 2013-12-27
My Shell Script looks like this .

# Copying the dat file to Pool Talk server
     ftp -n $EPT_SERVER_NAME <<!
     quote user $EPT_USER
     quote pass $EPT_PASSWORD
     binary
     cd /ssss/ept$ENV/ddd/in/mbs
     lcd /ffff/l7$ENV/sss/out/2006
     put SchA.dat
     !
 # Copying the dat file to BloomBerg server
     ftp -n $BBERG_SERVER_NAME <<!
     quote user $BBERG_USER
     quote pass $BBERG_PASSWORD
     binary
     cd /export/xxxx/wyyyy/zzz/ftp/dddd/mfdisclose/input/
     lcd /appl/l7$ENV/data/out/2006
     put MF_SCHEDA_*.dat
     !

As it is obvious from the above code, that i am trying to ftp file to  two  different servers. Now i have a new requirement that says that if ftp to the first server succeeds, then i don't  have to ftp the file to the next server.  To achive that i need to know the status of the ftp operation. Please let me know how to find the status of the ftp operation inside shell script.

I tried $?, which i heard will give me the status of the last shell command. but in this case , i get 0 all the time even if the ftp fails to succeed. Please let me know the other solutions.

Thanks
Subha
0
Comment
Question by:SubhaBabu
3 Comments
 
LVL 10

Accepted Solution

by:
Nukfror earned 100 total points
ID: 16997744
The return code from an FTP command doesn't really tell you much.  If you successfully connect to a remote FTP server, then the return code will always be 0.  The status of your file transfer within that FTP connection isn't reflected in the $? environment variable.

I'm no FTP guru but you since you're transferring the files in binary mode you could write an FTP that does nothing but ftp back into the Pool Talk server and get a file listing of the file you just transferred.  This should show the file size.  You capture this value and compare that against the file size on your system.  If the file sizes are the same, the file should have been transferred correctly.  Something like:


# Copying the dat file to Pool Talk server
     ftp -n $EPT_SERVER_NAME <<!
     quote user $EPT_USER
     quote pass $EPT_PASSWORD
     binary
     cd /ssss/ept$ENV/ddd/in/mbs
     lcd /ffff/l7$ENV/sss/out/2006
     put SchA.dat
     !
# Get status of the dat file at Pool Talk server
FTP_FILEINFO=$(# Copying the dat file to Pool Talk server
ftp -n $EPT_SERVER_NAME <<!
quote user $EPT_USER
quote pass $EPT_PASSWORD
cd /ssss/ept$ENV/ddd/in/mbs
ls SchA.dat
!)
FTP_FILESIZE=$(echo $FTP_FILEINFO | awk '{print $5}')
LOC_FILESIZE=$(ls -l SchA.dat | awk '{print $5}')
if [[ "X$FTP_FILESIZE" -eq "X$LOC_FILESIZE" ]]; then
  exit 0
fi

# Copying the dat file to BloomBerg server
     ftp -n $BBERG_SERVER_NAME <<!
     quote user $BBERG_USER
     quote pass $BBERG_PASSWORD
     binary
     cd /export/xxxx/wyyyy/zzz/ftp/dddd/mfdisclose/input/
     lcd /appl/l7$ENV/data/out/2006
     put MF_SCHEDA_*.dat
     !

I haven't tested the above obviously but it should present my point.  You may be wondering why I put "X" in front of the variable in my if statement.  This handles the condition of if either variable is equal to 0.  A zero value generally doesn't do well in an if statement in scripts unless specially handled.  Also, non-root users sometimes aren't allowed to execute list commands on FTP servers and you might not be able to see a directory listing in the directories you have listed above assuming the command doesn't out right fail.

The only true way to confirm that file was transferred correctly is to use checksums on both sides which you can't really do in this instance.

I'm quite curious to see what other folks have to say on this.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now