Skip to Content
Purpose
To explain how a developer can include a simple but comprehensive error handling mechanism within their kornshell script that includes an FTP connection and transfer.

Background:
In this scenario, we use a script that uses an environment file (xxxx.env) as a parameter to establish an ftp connection with a legacy system and transfer a file over. The variables are defined in the environment file and then called in the script. Specifically we use the environment file to feed the source directory, target directory, ftp login credentials, script name, and file name (please see bottom). The script is called via PI communication channel.

Solution Summary:
We are interested in performing error handling in the following areas:

1) Check parameter .env file

          a. Check if .env file is provided

          b. Check if parameter .env file has appropriate configuration

2) FTP error handling

          a. Check if ftp connection is successful

          b. Check if ftp transfer is successful

3) Archiving of .env files separately between error and successful executions

1a). Our first goal is to check the .env file and make sure it is provided. We use –z to check if the length of the first argument (the .env file) is 0 or not. If it is zero the next command is triggered which echos the error followed by an exit. Note: For our purposes we will enumerate any exit of a non-successful command as non zero starting with 1 and going upwards (e.x. exit 1, exit 2…etc.)

/wp-content/uploads/2013/06/pic1_226679.png

Response when no parameter provided (string =0):

/wp-content/uploads/2013/06/pic2_226680.png

1b.) Our second goal is to check if an appropriate .env file is in the location where we are executing the script. We use the “!–f” to see if the file does not exists or is not a regular file. If this is the case in this scenario we echo that it is not an appropriate file and call an alert (which is commented out in our example) and finally we call a function mv_error_file which is explained here.
/wp-content/uploads/2013/06/pic3_226681.png
Response if failure:

/wp-content/uploads/2013/06/pic4_226685.png

Response if successful:

/wp-content/uploads/2013/06/pic5_226686.png

2a) Here we wish to perform error handling within the ftp commands. The tricky part here is that the –f, -z, -e etc. commands in unix are not available for us since we are dealing within the ftp prompt. An exit status $? Is not of much use either since regardless of what happens within the ftp prompt, once a “bye” is declared it will be considered a success.

With this conundrum, it is best to make use of the ftp logs as the primary mechanism for error handling. First, we wish to generate a ftp log based on a prior ftp commands inputted into STD_DIR/SCRIPT_NAME. This is overwritten to ftp –dinv. The –dinv ensures that the log is verbose and comprehensive for our purposes. Finally this will be inputted into a .log file using STD_DIR/INTERFACE_NAME identified by the .env file. This single command line effectively generates a log file for us to use for our handling

Now that we have generated a log file we can search the log for key words noting a successful connection. Note: It is recommended to execute a script with a successful scenario to view what key words can be used for this search. In this case “S7PISPM1 is logged on” is a key word identifying that the ftp connection is successful. We use the command fgrep to search for this string within the log file. In the case that it Is not found an error is echoed, a pager alert is called (commented out for this example) and a function is called mv_error_file.

/wp-content/uploads/2013/06/pic6_226687.png

Response if successful ftp connection:

/wp-content/uploads/2013/06/pic7_226688.png

Response if unsuccessful ftp connection:

/wp-content/uploads/2013/06/pic8_226689.png

2b) The next step is to see whether the “put” transfer was successful. Similar to the previous check, we can use search through the log for keywords to make a call on whether the transfer was successful.  In this instance our keywords are “Transfer completed successfully.” If the put is unsuccessful then the pager alert will be called as well as the function mv_error_file.

/wp-content/uploads/2013/06/pic9_226690.png

Response if successful:

/wp-content/uploads/2013/06/pic10_226691.png

 

Response if unsuccessful:

/wp-content/uploads/2013/06/pic11_226692.png

3. We have been using the function mv_error_file in many instances of errors. The rationale behind this is to move error environment files in separate file server with a date stamp so that it can be troubleshooted afterwards. With this in mind a simple function is created that moves the .env file the directory of our choice and renames it with the .env file name concatenated with the current date.

/wp-content/uploads/2013/06/pic12_226693.png

In the scenario where all checks pass and the file is successfully transferred we use this code. This checks the exit status of the move command. In the scenario where it does not equal 0, the pager alert is called and it is moved to a separate directory via calling our mv_error_file function and finally exiting.

/wp-content/uploads/2013/06/pic13_226694.png

Final Thoughts:
I hope you find these examples useful in your practical application. The premise of this blog is to show how comprehensive we can bee in our error handling with both UNIX script and ftp prompt.

Kornshell Script:

#!/bin/ksh

CURR_DATE=`date ‘+%Y%m%d_%H%M%S`

# Create function to move error file in separate directory with date stamp
mv_error_file()
{
     mv $FTP_CONF_FILE /SPMfile/out/XS3/Error/${FTP_CONF_FILE}_${CURR_DATE}
}

# Check .env file provided or not.
[[ -z $1 ]] && echo “Error: Usage:- $0 [ftp-config file]” && exit 1

FTP_CONF_FILE=”$1″
# Check if appropriate configuration file
if [ ! -f $FTP_CONF_FILE ];then
     echo “$FTP_CONF_FILE doesn’t seem to be an appropriate ftp configuration file”
     # /apps/home/pageCurrent.ksh “$FTP_CONF_FILE doesn’t seem to be an appropriate ftp configuration file for Planning Alert”
     mv_error_file
     exit 1
else
     echo “Appropriate ftp configuration file found”
fi

# source ftp configuration
. $FTP_CONF_FILE

# Generate FTP Script
> ${STD_DIR}/${SCRIPT_NAME}
echo “open $TARGET_HOSTSERVER” >> ${STD_DIR}/${SCRIPT_NAME}
echo “user $TARGET_FTPLOGIN $TARGET_FTPPASSWORD” >> ${STD_DIR}/${SCRIPT_NAME}
echo “$QUOTE_CMD” >> ${STD_DIR}/${SCRIPT_NAME}
echo “put ${STD_DIR}/$SOURCE_FILENAME $TARGET_FILENAME” >> ${STD_DIR}/${SCRIPT_NAME}
echo “bye” >>${STD_DIR}/${SCRIPT_NAME}

ftp -dinv < ${STD_DIR}/${SCRIPT_NAME} >> ${STD_DIR}/${INTERFACE_NAME}.LOG
# Check if ftp connection is successful
FTP_SUCCESS_MSG=”S7PISPM1 is logged on”
if fgrep “$FTP_SUCCESS_MSG” ${STD_DIR}/${INTERFACE_NAME}.LOG ;then
     echo “ftp OK”
else
     echo “ftp error”
     #/apps/home/pageCurrent.ksh “There is an issue in the FTP connection for Planning Alert”
     mv_error_file
     exit 2
fi

# Check if file put is successful
FILEPUT_SUCCESS_MSG=”Transfer completed successfully”
if fgrep “$FILEPUT_SUCCESS_MSG” ${STD_DIR}/${INTERFACE_NAME}.LOG ;then
     echo “put OK”
else
     echo “put error”
     #/apps/home/pageCurrent.ksh “There is an issue in the file put for Planning Alert”
     mv_error_file
     exit 3
fi

echo “Interface Last Run Time: ${CURR_DATE}”  >>${STD_DIR}/${INTERFACE_NAME}.LOG

#Move File to Archive Directory
mv ${STD_DIR}/${SOURCE_FILENAME} ${STD_DIR}/archive/${SOURCE_FILENAME}_$CURR_DATE.txt
if [ $? -ne 0 ];then
    echo “the file was not successfully moved to the archive directory”
    # /apps/home/pageCurrent.ksh “Lognet File not successfully moved to archive directory”
    mv_error_file
    exit 4
fi

mv ${STD_DIR}/${INTERFACE_NAME}.LOG ${STD_DIR}/archive/${INTERFACE_NAME}_$CURR_DATE.LOG
rm ${STD_DIR}/${SCRIPT_NAME}

Environment File:

#!/bin

#. ~/.profile

INTERFACE_NAME=XXXX

STD_DIR=”/xxx/out/xxx”

SCRIPT_NAME=xxxx.scr

SOURCE_FILEDIR=”/xxxx/out/xxx/”

TARGET_HOSTSERVER=’host.target.company.com’

TARGET_FTPLOGIN=’S7PISPM1′

TARGET_FTPPASSWORD=’xxxxx’

SOURCE_FILENAME=abc123

TARGET_FILENAME=”xxxxx'”

QUOTE_CMD=”xxxxxx”

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply