Tuesday, March 13, 2012

SHELL SCRIPT TO PERFORM AUTO FILE TRANSFER PROTOCOL (FTP)


This post will display another reusable code that can be registered as concurrent program in Oracle Apps without any modification. This program can be used to extract a file from external server using FILE TRANSFER PROTOCOL (FTP) using shell script. I hope this sample program will be very handy to my fellow developers.

SCRIPT:
##########################################################################
# DESCRIPTION      : FTP program for sending Extract File to Other server#
##########################################################################

FILE_DIR=`echo $1|cut -f8 -d'"'` #Parameter1: Source file Directory
FILE_DIR=`echo $FTP_PATH/$FILE_DIR|tr -d " "`
MOV_DIR=`echo $1|cut -f10 -d'"'` #Parameter2: Destination file Directory
MOV_DIR=`echo $FTP_PATH/$MOV_DIR|tr -d " "`
LOG_FILE='Log_ftp.log'
LOG_FILE=`echo $FTP_PATH/$LOG_FILE|tr -d " "`
V_EMAIL=`echo $1|cut -f12 -d'"'`
INST=`echo $1|cut -f14 -d'"'`


cd $FILE_DIR

echo $FILE_DIR
echo "Running Ftp Script..."


echo "File directory is $FILE_DIR"
echo "Move directory is $MOV_DIR"
echo "Log File is $LOG_FILE"
echo "Email is $V_EMAIL"
echo "Instance is $INST"


if [ "$INST" = 'PROD' ]
then
cd $FILE_DIR

V_FILE_COUNT=`ls -l FILE*.dat| wc -l`
if [ $V_FILE_COUNT -gt 0 ]
then
    for FILE in FILE*.dat
    
    do
        if [ -f $LOG_FILE ]
        then
            rm $LOG_FILE
        fi
        echo Orig File Name $FILE
        
        V_FILE_NAME=`ls $FILE | cut -f1,2 -d '.'`
        V_RC_COUNT=`wc -l <$FILE`
        echo File record count $V_RC_COUNT        
        V_FILE=$V_FILE_NAME.$V_RC_COUNT.dat
        
        mv $FILE $V_FILE
        
        echo New File Name $FILE
        
        LOCAL_FILE=$V_FILE
        TARGET_FILE=$V_FILE
        echo The file to be FTP-ed is $LOCAL_FILE
        
        V_FILE_SIZE=`wc -c $LOCAL_FILE | cut -f1 -d" "`
        echo The File Size is $V_FILE_SIZE
                
        V_COUNT_BEFORE=`wc -l <$LOCAL_FILE`
        echo Total No. of Records before FTP is $V_COUNT_BEFORE
        
        FTP_PARAMS=`sqlsecure sqlplus -s APPS/%APPS% << EOF
        set heading off
        set feedback off
        SELECT host||'~'||user_id||'~'||password||'~'||dest_path||'~'
        FROM ftp_table
        WHERE interface_code = 'OEFTP';
        quit;
        EOF`
        for FTP_REC in $FTP_PARAMS
        do 
            FTPHOST=`echo $FTP_REC | cut -f1 -d'~'|tr -d " "`
            FTPUSER=`echo $FTP_REC | cut -f2 -d'~'|tr -d " "`
            FTPPASS=`echo $FTP_REC | cut -f3 -d'~'|tr -d " "`
            FTPDIR=`echo $FTP_REC | cut -f4 -d'~'|tr -d " "`
            echo -----FTP-----
            echo Server is $FTPHOST            
            echo Server directory is $FTPDIR
            echo Server user id is $FTPUSER

        ftp -n> $LOG_FILE <<-EOF
            open $FTPHOST
            user $FTPUSER $FTPPASS
            hash
            verbose
            cd $FTPDIR
            ascii
            put $LOCAL_FILE $TARGET_FILE
            EOF
    
        V_SUCCESS=`grep 'Transfer complete' $LOG_FILE| cut -c 1-3`
        if [ $V_SUCCESS -eq "226" ]
        then
            echo FTP happened for $LOCAL_FILE to host :$FTPHOST
        else
            echo $LOCAL_FILE is not transfered to host :$FTPHOST
        fi

        done

        if [ $V_SUCCESS -eq "226" ] 
        then
            mv $LOCAL_FILE $MOV_DIR/$LOCAL_FILE.bak
        fi
        echo --------------------------------------    
    done
else
    echo "No files available to transfer"
fi
fi


0 Responses to “SHELL SCRIPT TO PERFORM AUTO FILE TRANSFER PROTOCOL (FTP)”

Post a Comment

Disclaimer

The ideas, thoughts and concepts expressed here are my own. They, in no way reflect those of my employer or any other organization/client that I am associated. The articles presented doesn't imply to any particular organization or client and are meant only for knowledge Sharing purpose. The articles can't be reproduced or copied without the Owner's knowledge or permission.