Solved

How to find the status of FTP operation inside shell script

Posted on 2006-06-27
3
522 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

808 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