Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing the status of an FTP call in a UNIX script

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
US
I recently discovered the FTP step of one of my UNIX shell scripts does not capture error codes:
Code:
echo '----------------------------------------------------------' 
echo '                                                          '
echo ' STEP02 -- UPLOAD LOAD FILE TO TMIC (FTP.RCP.PS44.LOAD)   '
echo '                                                          '
echo '----------------------------------------------------------'
echo "Executing ftp/ftp44..."
cd $HOME
ftp -i -n <ftp/ftp44 

#Check if last command is successful.
if [ $? -ne 0 ]
then
echo "An error occurred while FTPing rcp44 load file from server to TMIC. "
echo ' ' | mailx -s "rcp44 has abended!! "`date '+%m/%d/%y'` DIS < rcp44.out
exit 
fi
 
echo "FTP was done at `date`."
Where ftp44 is another file with this script
Code:
open TMIC
user user password
put /u003/tmp/rcp44.dat RCP.PS44.LOAD
quit
I saw the FTP script in the FAQ and had a couple questions.
[ol]
[li]Am I correct if I said no exceptions are ever caught because it always returns "0"?[/li]
[li]What does the "i" parameter in my call do? I read some online manuals but could not find an explaination of what it does, exactly.[/li]
[li]Is there a reason the FTP script in the FAX uses "verbose" in the FTP script instead of the "v" flag?[/li]
[li]Why does the example use "exec" instead of calling another file, like mine?[/li]
[/ol]I am sure I will have more questions in the future. :)
 
1 - yes . normal ftp clients on unix do not through out exceptions.
2 - -i - turns off interactive mode
3 - there is no verbose on code you shown
4 - there is no exec on code you shown.


As for error handling - you will need to output the results to a file, and then parse it for errors.
All ftp messages will have 3 digits code followed by a space.
Some will be informational others will be errors. search the net for codes available as those are common to most ftp implementations.

If you can change to use sftp or ftps - those do have normally error handling.



Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
fredericofonseca said:
1 - yes . normal ftp clients on UNIX do not through out exceptions.
2 - -i - turns off interactive mode
3 - there is no verbose on code you shown
4 - there is no exec on code you shown.

2) What is "interactive mode"? The one in the FAQ doesn't use it...
3) I was referring to the example in the FAQ
4) I was referring to the example in the FAQ

Code:
#!/bin/ksh
bindir="/home/ftp/bin"
logdir="/home/ftp/log"
ftp="/usr/bin/ftp"
Email="[Some E-mail Address]"
IP="10.124.33.29"
user="[user-id]"
pass="[password]"
ifile="[source file]"
ofile="[destination file]"
type="asc"
myname=`basename $0`
verbose="verbose"
dd=`date +%d`
log="$logdir/$myname.$dd.log"
host=`hostname`
rc=0
boj=`date`

exec 1>$log 2>&1

echo "\n#####  Begin FTP Parameters  #####\n"
echo "Program Name:        $myname"
echo "Process ID:          $$"
echo "Log file name:       $log"
echo "Email:               $Email"
echo "Source Machine       $host"
echo "Destination Machine: $IP"
echo "User ID:             $user"
echo "Password:            ##############"
echo "Source File:         $ifile"
echo "Destination File:    $ofile"
echo "\n#####  End FTP Parameters  #####\n\n"

echo "#####  Begin FTP Session  #####\n"

echo "open $IP
      user $user $pass
      $verbose
      $type
      put $ifile $ofile
      close
      quit" |$ftp -n

echo "\n#####  End FTP Session  #####"

foo=`grep -i "cannot" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="Destination file does not exist\n"
fi
foo=`grep -i "does not" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Source file does not exist\n"
fi
foo=`grep -i "killed" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}File transfer process has abended\n"
fi
foo=`grep -i "space" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Ran out of disk space before completion of copy\n"
fi

if [ "$rc" -eq "0" ] ; then
   status="Successful"
fi

eoj=`date`
echo "\nJob start time:   $boj"
echo "Job end time:     $eoj"
echo "\nResult code:      $rc ($status)"

mail -s "FTP results from $myname" $Email <$log
 
2) When you try to send or receive multiple files with a wildcard using "[tt]mget[/tt]" or "[tt]mput[/tt]", [tt]ftp[/tt] will prompt you for every file before sending it or receiving it. Turning off interactive mode stops this prompting and just sends ot receives all files that match the wildcard.

4) That [tt]exec[/tt] command reassigns [tt]stdin[/tt] and [tt]stdout[/tt] to the log file ([tt]$log[/tt]). From that point on, and normal or error output from the script will go to that file.






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top