?
Solved

How to find the status of FTP operation inside shell script

Posted on 2006-06-27
3
Medium Priority
?
527 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 400 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.
Suggested Courses

771 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