Solved

How to find the status of FTP operation inside shell script

Posted on 2006-06-27
3
518 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

773 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