Script not working.

Hi,

I am a DBA, worked on Windows platforms for past 6 years, and now shifted in environment where HP UX is OS environment.

I have task to complete which involves Unix script to be prepared. This script should FTP the file to the destination server and if this FTP fails, then it should write in syslog file. For the same purpose, I have prepared following script.

#!/usr/bin/sh
# testftp : Script to transfer file from SAP to wowftp server
#-------------------------------------------------------------------

# Timestamp
echo ------------------------------------ >> /interface/logs/alhftplog.txt
echo `date` Start of FTP to WOW FTP >> /interface/logs/alhftplog.txt
echo ----------------------------------- >> /interface/logs/alhftplog.txt

# FTP

ftp -vn HOST NAME 2> /interface/logs/ftpfailedlog.log <<END_SCRIPT >> /interface/logs/ftplog.txt
quote USER USER NAME
quote PASS USER PASSWORD
ascii
prompt off
lcd /interface/outbound
cd /test
mput FILE PATTERN*
bye
END_SCRIPT

# End of FTP

EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
logger -p local6.info -t XXTIV9999E9 "FTP to Host Failed"
else
echo `date` "FTP Successful" >> /interface/logs/ftplog.txt
fi

I am able execute above-mentioned screen and not experiencing any error. However, when I check in the system log file, no error message is being recorded.

To test this problem, I have executed �logger� command from OS prompt and I have found that error is being written in the syslog file.

Please help me to resolve this problem.

Thanks,

Best regards,

Nishchal Nagre.

  1. Please use the code environment around your script for readability
  2. [quote="nishchal_nagre"]
    I am able execute above-mentioned screen and not experiencing any error. However, when I check in the system log file, no error message is being recorded.
    [/quote]
    So your problem is....?

Hi,

My problem is, below given part of my script is not working. After execution of the script it has not spitting any error.

EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
logger -p local6.info -t XXTIV9999E9 "FTP to Host Failed"
else
echo `date` "FTP Successful" >> /interface/logs/ftplog.txt
fi

Thanks,

Best regards,

Nishchal Nagre.

Ok, I'm going to assume that you mean that the FTP transfer failed, but you didn't get an error message.

Try changing the lines

EXITSTATUS=$?
if [ $EXITSTATUS != "0" ]

to

if [ $? != 0 ]

or even

if [ ! $? ]

ftp will not throw an error if it connects and runs commands then exits ok. You will need to capture the error from ftp itself. So you need to look at the stderr from you ftp command and see if the error on file transfer failed.Do you get anything in /interface/logs/ftpfailedlog.log?

I want to accomplish the same thing as Nischal, if anybody can shed light as to what's wrong with my script, I would appreciate it :slight_smile:

Here is what i want to do, ftp a file to a server then if there are any errors, an email shall be sent to my account stating that the ftp process failed. However as BubbaJoe mentioned, eventhough I get an ftp error, the return code is still zero (upon checking with $?). I need to be able to capture the error code since this will be the basis for my if statement and for the script that will run after the ftp. Initially I tried this (which didn't work):

ftp -nv server.com<<-EOF
user USER Password                          
prompt
ascii
put testfile /export/dir/test/testfileftp               
bye
EOF
 
#Check if FTP is successful or not
ERRCD=$?
if [ $ERRCD -ne 0] then
      echo "The FTP process failed" | mailx -s "The FTP process failed with error code $ERRCD on $(date '+%m-%d:%H:%M')" myemail@administrator.com
            print �ERRCD=${ERRCD}� << /tmp/Send_errcd.log
exit 1        
fi

I was able to search something in the net which seemed like a good idea, since in his code, he checked the return code, the bytes transferred and transfer complete as the basis for whether there was an error:

ftp -nv server.com<<-EOF
user USER Password                          
prompt
ascii
put testfile /export/dir/test/testfileftp               
bye"|ftp -iv > ftpreturn.log

EOF
 
#Check if FTP is successful or not
ftpresult=$?
bytesindatafile=wc -c testfile | cut -d " " -f 1
bytestransferred=grep -e '^[0-9]* bytes sent' ftpreturn.log | cut -d " " -f 1
ftptransfercomplete=grep -e '226 ' ftpreturn.log | cut -d " " -f 1
 
echo "-- FTP result code: $ftpresult" >> ftpreturn.log echo "-- bytes in datafile: $bytesindatafile bytes" >> ftpreturn.log echo "-- bytes transferred: $bytestransferred bytes sent" >> ftpreturn.log
if [ "$ftpresult" != "0" ] || [ "$bytestransferred" != "$bytesindatafile" ] || ["$ftptransfercomplete" != "226" ] then
          echo "The CANS process failed" | mailx -s "The FTP process failed with error code $ERRCD on $(date '+%m-%d:%H:%M')" myemail@administrator.com
fi

However in that code block above, i get the error that the -c and -e commands are not valid (I am using ksh). Please help me dissect the code or if you have a suggestion on how I could check the success or not of an ftp, as well as use that error code for the email to be sent and as the basis for whether my next script will be kicked off, i would highly appreciate it.