Skip to Content
Author's profile photo Former Member

Error Handling in Kornshell Script with FTP transfer

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.

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.)


Response when no parameter provided (string =0):


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.
Response if failure:


Response if successful:


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.


Response if successful ftp connection:


Response if unsuccessful ftp connection:


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.


Response if successful:



Response if unsuccessful:


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.


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.


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:


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

# Create function to move error file in separate directory with date stamp
     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

# 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”
     exit 1
     echo “Appropriate ftp configuration file found”

# source ftp configuration

# Generate FTP Script
echo “bye” >>${STD_DIR}/${SCRIPT_NAME}

# Check if ftp connection is successful
FTP_SUCCESS_MSG=”S7PISPM1 is logged on”
     echo “ftp OK”
     echo “ftp error”
     #/apps/home/pageCurrent.ksh “There is an issue in the FTP connection for Planning Alert”
     exit 2

# Check if file put is successful
FILEPUT_SUCCESS_MSG=”Transfer completed successfully”
     echo “put OK”
     echo “put error”
     #/apps/home/pageCurrent.ksh “There is an issue in the file put for Planning Alert”
     exit 3

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

#Move File to Archive Directory
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”
    exit 4


Environment File:


#. ~/.profile











Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.