Solved

How to find the status of FTP operation inside shell script

Posted on 2006-06-27
3
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

749 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