Unable to obtain the desired output


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to obtain the desired output
# 1  
Oracle Unable to obtain the desired output

Hi,

I am unable to get beyond the exit function. The shell script is used to look for masked files and copy paste them to another location. Please refer to the code below for more information.

Thanks

Brinjit

Code:
 
#!/usr/bin/ksh
#########################################################################
#  script name: cdg_file_load.ksh
#  Created    : March 29, 2013
#  Author     : David Ayotte
#
#  Find new files for the file type and period that is passed in in the form
#       of a lookup key that is used to determine what directory and file masks
#       to look for. The lookup file is $FP_ENV/CDG_Lookup.txt
#
#  Revision History: (include date, username, and brief description in
#     descending order)
#########################################################################
#
#  Function definitions
#
#  Usage Function
#
function usage_msg {
   echo "\n---> Usage: \n"
   echo "---> cdg_file_load.ksh -k< key > "
        echo "          -k is a Key lookup value in the $FP_ENV/CDG_Lookup.txt file."
#       grep -v "#"  $lookupFile | cut -d"|" -f1 | sort | uniq
        echo ""
   exit_func
}
#
#  Exit Function
#
function exit_func {
   echo "---> Running exit function..."
   exit $rc
         #  update_job_table only when $rc != 0
}
#
#   Make sure the file is static
#
function is_file_changing {
        baseName=`basename ${sourceFile}`
        timeFile=${FP_LOGDIR}/${baseName}_tmstp
        touch ${timeFile}
        echo "Time Compare file is ${timeFile}"
        echo "---> Sleeping to see if file changes in 20 seconds "
        sleep 20 
        while [ ${sourceFile} -nt ${timeFile} ]
        do  
                touch ${timeFile}
                echo "-> Sleeping 20 more seconds...attempt $i"
                sleep 20 
                let i=$i+1
                if [ $i -gt 60 ] #  I am sick of waiting
                then
                        rc=24
                        exit_func
                fi
        done
        echo "---> File is found and not changing. "
}
#
#       Execute Informatica workflow to load new file.
# 
function load_file {
        #
        #   Determine that file is not changing.
        #
        # TEST is_file_changing
        #
        #    Creat link to file, much quicker than copying to local name.
        #
        if [ -f ${FP_FTP_DOWN}/${staticFile} or -h  ${FP_FTP_DOWN}/${staticFile} ]
        then
                rm  ${FP_FTP_DOWN}/${staticFile}  # no need to check return code, the ln command will fail.
        fi
        echo "---> Creating link from static file name ${staticFile} "
        ln -s  ${sourceFile} ${FP_FTP_DOWN}/${staticFile} 
        rc=$?
        if [ $rc -ne 0 ]
        then
                echo "---> ERROR: Unable to create link. "
                rc=30
                exit_func
        fi
        #
        #   Execute Informatica workflow
        #
        export parmFile=${FP_PARMFILES}/${workFlow}.par
        echo "---> Executing Informatica workflow ${workFlow} "
cmd="pmcmd startworkflow -wait -sv ${FP_SERVICE} -d ${FP_DOMAIN} -uv FP_INF_USER -pv FP_INF_PSWD " 
cmd=$cmd" -paramfile ${parmFile} -f ${FP_FOLDER} ${workFlow} "
echo "--> Command: $cmd"
  $cmd
  rc=$?
  if [ $rc -ne 0 ]
  then
                echo "---> ERROR: Error executing Informatica Workflow."
                rc=30
                exit_func
        fi
}
#
#   Determine if this file has been loaded already.
#
function has_file_been_loaded {
        alreadyLoaded=0 # False - File has not been loaded
        cnt=`grep -c ${sourceFile} ${cdgFileList}` 
        if [ $cnt -eq 0 ]
        then
                echo "---> This file has not been loaded "
        else
#               echo "---> This file has already been loaded "
                alreadyLoaded=1 
        fi
}
#
#   Check for files in a directory
#
#   More like, process a file mask for a directory for a workflow. Several file types
#        can be found in each directory, but must be processed by a different Informatica
#        workflow.
#
function process_a_directory {
        echo "Checking ${FP_CDG_CFR}${searchPath}/${searchMask}"
#       for sourceFile in `ls -1 ${FP_CDG_CFR}${searchPath}/${searchMask}*`
        for sourceFile in `find ${FP_CDG_CFR}${searchPath} -name "${searchMask}" -type f -mtime -5`
        do
                echo "---> Checking $sourceFile "
                has_file_been_loaded
                if [ ${alreadyLoaded} -eq 0 ]
                then
                        # for Jeff to add later  update_job_table
                        load_file
                        let fileCntr=$fileCntr+1
                        echo $sourceFile >> ${cdgFileList} # So we won't load it again
                        #  for Jeff to add later update_job_table
                fi
        done
}
#
#    extract values from lookup record
#
function get_values {
        #  Extract values 
        searchPath=`echo $rec | cut -d"|" -f2`
        searchMask=`echo $rec | cut -d"|" -f3`
        staticFile=`echo $rec | cut -d"|" -f4`
        workFlow=`echo $rec | cut -d"|" -f5`
        # Display values
        echo "\n---> Search Path = $searchPath"
        echo "---> Search Mask = $searchMask"
        echo "---> Static File = $staticFile"
        echo "---> Workflow    = $workFlow"
}
#
#   Find new files and load them
#
function find_and_load_new_files {
        echo "---> attempting to process $keyValue "
        lookupValuesFound=0
        for rec in `grep "^${keyValue}|"  $lookupFile`
        do
                get_values
                let lookupValuesFound=$lookupValuesFound+1
      process_a_directory
   done
}
#
#  Command Line Parsing
#
export fileCntr=0
export lookupFile=${FP_ENV}/CDG_Lookup.txt
export cdgFileList=$FP_FTP_DOWN/cdg_loaded_files.txt   # list of files already loaded.
#  remove this next line after testing is done
#
# export FP_CDG_CFR=/fpapps/op/qa/ftproot/cdgtest
#
if [ $# -ne 1 ]
then
   usage_msg
fi
while getopts "k:" option
   do
   case  "$option" in
   k) keyValue="$OPTARG"
      ;;
   [?])
      usage_msg
      ;;
   esac
   done
#
#
if [ "$keyValue" = "" ]
then
        exit_func
fi
#
find_and_load_new_files
#
if [ $lookupValuesFound -eq 0 ]
then
        echo "\n---> ERROR: No values found in lookup file. "
        rc=30
else
        echo "\n---> $lookupValuesFound lookup values found"
fi
#
echo "\n---> Total Files processed is $fileCntr \n"
exit_func


The actual requirement is specified below:

Code:
 
aCR3027 – OP – Operational Data Store - Sophia Decommission – CDG Processing notes
Create date:      June 20, 2013
Author:           David Ayotte

UNIX Systems:
Development:      Host: nh1a7t05 - Unix ID: opdev
QA:               Host: nh1a7t05 - Unix ID: opqa
Production:       Host: nh1a7p09 - Unix ID: opuser

Informatica Environments:

      Development:      Repository:  FP_DEV_REPO - Folder:  OP
      QA:               Repository:  FP_QA_REPO - Folder:  OP
      Production:       Repository:  FP_PRD_REPO_P04 – Folder: OP


Project Goal:

      Load files from CDG directories for multiple file types at different times of the month (cycles).  These cycles are listed below:

CYCLE04
CYCLE06
CYCLE07
CYCLE10
CYCLE13
CYCLE19
CYCLE25
DAILY
EOM

      Each cycle corresponds to a Control-M calendar with similar names as shown in the following diagram.   The calendars will determine when to check for files in the directories that correspond to the cycles defined above:




A lookup file called CDG_Lookup.txt is used to drive the process. This file resides in /fpapps/op/qa/env on nh1a7t05.  UNIX scripting and Control-M work is not done in the development directories due to the environment not being static. 

Below are the entries in the lookup file for CYCLE04. This is a pipe “|” delimited file with the fields from left to right being: Key | Directory to check | File Mask to check | Static File | Informatica workflow | Calendar (for reference only)

Key: This is to be passed to the script on the command line of the Control-M job.  The script will use this to determine what and how to process this cycle.  

Directory to check: These are the subdirectories under /fpapps/cfr/CDG that the script will check for files to load.

File Mask to Check: Files found that match this format will be loaded.

Static File: Each file that is found to match the format mask will be copied, one at a time, to the static file name. After this copy, the Informatica Workflow will run and load the static file name. 

Informatica Workflow:  This is the name of the Informatica workflow that will run to load the static file. 

Calendar:  This is for reference only.  When this has a value of multiple, you may find this directory and file mask under more than one cycle.

CYCLE04|/Reports/0570/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0570/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0570/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0570/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0570/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0570/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0571/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0572/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0581/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0582/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0020.D*.T*.CSV|AC0020.CSV|WF_LOAD_FF_TEMP_AC0020|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0025.D*.T*.CSV|AC0025.CSV|WF_LOAD_FF_TEMP_AC0025|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0068.D*.T*.CSV|AC0068.CSV|WF_LOAD_FF_TEMP_AC0068|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0730.D*.T*.CSV|AC0730.CSV|WF_LOAD_FF_TEMP_AC0730|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0760.D*.T*.CSV|AC0760.CSV|WF_LOAD_FF_TEMP_AC0760|CDG_Cycle4|
CYCLE04|/Reports/0583/04th|AC3.C*.AC0780.D*.T*.CSV|AC0780.CSV|WF_LOAD_FF_TEMP_AC0780|CDG_Cycle4|
CYCLE04|/Reports|Processed_AC3.C*.AC0877J.D*.T*.CSV|AC0877.CSV|WF_LOAD_FF_TEMP_AC0877|Multiple|


The script to run the load is called cdg_file_load.ksh and resides in the following directory on nh1a7t05:

/fpapps/op/qa/prod/scripts/cdg

and in the following directory on nh1a8p04:

/fpapps/op/prod/scripts/cdg

The only command line option for the script is –k<key>, where key is the key value from the CDG_Lookup.txt file (such as CYCLE04).

A sample command line for Control-M would look like this:

batchjob OP %%JOBNAME ${FP_SCRIPTS}/cdg/cdg_file_load.ksh –eCYCLE04


The above command line example will work in QA and in production on nh1a8p04. 

Misc:

A file called cdg_loaded_files.txt exists in the following directory on nh1a7t05: 

/fpapps/op/qa/ftproot/ftpdown 

The cdg_loaded_files.txt file contains a list of all files and the full directory to each file that has been loaded.  This is updated after a file is loaded without errors. This way, if a load fails, it is not marked as loaded. 

The need for this file is because we do not have the ability to remove files from the cfr (Central File Repository).  I suppose we could have built an entire archive directory structure that matches what is on the cfr, but this file should work okay for a long time.   

Since the find command in the load script only goes back five days, this file can eventually have old rows removed from the beginning. Much care should be taken before this is done and a script should probably be written to do this. For example, find all files on the cfr older than 5 days and remove them from this lookup file, and remove any files from this lookup that do not exist in the cfr. 


# 2  
Have you looked at the ksh man page? What do you expect the exit command to do? The ksh exit command terminates your shell script. You can't run any commands in your script after you exit.

If you're going to add an update_job_table function; you need to call it before you exit; not where you have the comment:
Code:
         #  update_job_table only when $rc != 0

# 3  
Unable to obtain the desired output

Hi,

I made the necessary changes but still not able to go beyond the exit function:

Code:
 
#!/usr/bin/ksh
#########################################################################
#  script name: cdg_file_load_test.ksh
#  Created    : July 25, 2013
#  Author     : Brinjit Velu
#
#  Find new files for the file type and period that is passed in in the form
#       of a lookup key that is used to determine what directory and file masks
#       to look for. The lookup file is $FP_ENV/CDG_Lookup.txt
#
#  Revision History: (include date, username, and brief description in
#     descending order)
#########################################################################
#
#  Function definitions
#
#  Usage Function
#
function usage_msg {
   echo "\n---> Usage: \n"
   echo "---> cdg_file_load_test.ksh -k< key > "
        echo "          -k is a Key lookup value in the $FP_ENV/CDG_Lookup.txt file."
#       grep -v "#"  $lookupFile | cut -d"|" -f1 | sort | uniq
        echo ""
   exit_func
}
#
#  Exit Function
#
function exit_func {
   echo "---> Running exit function..."
         if [ $? -ne 0 ]
              then 
                                  update_job_table
         
         fi
        
   exit $rc
         #  update_job_table only when $rc != 0
}
#
#   Make sure the file is static
#
function is_file_changing {
        baseName=`basename ${sourceFile}`
        timeFile=${FP_LOGDIR}/${baseName}_tmstp
        touch ${timeFile}
        echo "Time Compare file is ${timeFile}"
        echo "---> Sleeping to see if file changes in 20 seconds "
        sleep 20 
        while [ ${sourceFile} -nt ${timeFile} ]
        do  
                touch ${timeFile}
                echo "-> Sleeping 20 more seconds...attempt $i"
                sleep 20 
                let i=$i+1
                if [ $i -gt 60 ] #  I am sick of waiting
                then
                        rc=24
                        exit_func
                fi
        done
        echo "---> File is found and not changing. "
}
#
#       Execute Informatica workflow to load new file.
# 
function load_file {
        #
        #   Determine that file is not changing.
        #
        # TEST is_file_changing
        #
        #    Creat link to file, much quicker than copying to local name.
        #
        if [ -f ${FP_FTP_DOWN}/${staticFile} or -h  ${FP_FTP_DOWN}/${staticFile} ]
        then
                rm  ${FP_FTP_DOWN}/${staticFile}  # no need to check return code, the ln command will fail.
        fi
        echo "---> Creating link from static file name ${staticFile} "
        ln -s  ${sourceFile} ${FP_FTP_DOWN}/${staticFile} 
        rc=$?
        if [ $rc -ne 0 ]
        then
                echo "---> ERROR: Unable to create link. "
                rc=30
                exit_func
        fi
        #
        #   Execute Informatica workflow
        #
        export parmFile=${FP_PARMFILES}/${workFlow}.par
        echo "---> Executing Informatica workflow ${workFlow} "
cmd="pmcmd startworkflow -wait -sv ${FP_SERVICE} -d ${FP_DOMAIN} -uv FP_INF_USER -pv FP_INF_PSWD " 
cmd=$cmd" -paramfile ${parmFile} -f ${FP_FOLDER} ${workFlow} "
echo "--> Command: $cmd"
  $cmd
  rc=$?
  if [ $rc -ne 0 ]
  then
                echo "---> ERROR: Error executing Informatica Workflow."
                rc=30
                exit_func
        fi
}
#
#   Determine if this file has been loaded already.
#
function has_file_been_loaded {
        alreadyLoaded=0 # False - File has not been loaded
        cnt=`grep -c ${sourceFile} ${cdgFileList}` 
        if [ $cnt -eq 0 ]
        then
                echo "---> This file has not been loaded "
        else
#               echo "---> This file has already been loaded "
                alreadyLoaded=1 
        fi
}
#
#   Check for files in a directory
#
#   More like, process a file mask for a directory for a workflow. Several file types
#        can be found in each directory, but must be processed by a different Informatica
#        workflow.
#
function process_a_directory {
        echo "Checking ${FP_CDG_CFR}${searchPath}/${searchMask}"
#       for sourceFile in `ls -1 ${FP_CDG_CFR}${searchPath}/${searchMask}*`
        for sourceFile in `find ${FP_CDG_CFR}${searchPath} -name "${searchMask}" -type f -mtime -5`
        do
                echo "---> Checking $sourceFile "
                has_file_been_loaded
                if [ ${alreadyLoaded} -eq 0 ]
                then
                        function update_job_table { 
                        # for Jeff to add later  update_job_table
                        load_file
                        let fileCntr=$fileCntr+1
                        echo $sourceFile >> ${cdgFileList} # So we won't load it again
                        #  for Jeff to add later update_job_table
                }
                fi
        done
}
#
#    extract values from lookup record
#
function get_values {
        #  Extract values 
        searchPath=`echo $rec | cut -d"|" -f2`
        searchMask=`echo $rec | cut -d"|" -f3`
        staticFile=`echo $rec | cut -d"|" -f4`
        workFlow=`echo $rec | cut -d"|" -f5`
        # Display values
        echo "\n---> Search Path = $searchPath"
        echo "---> Search Mask = $searchMask"
        echo "---> Static File = $staticFile"
        echo "---> Workflow    = $workFlow"
}
#
#   Find new files and load them
#
function find_and_load_new_files {
        echo "---> attempting to process $keyValue "
        lookupValuesFound=0
        for rec in `grep "^${keyValue}|"  $lookupFile`
        do
                get_values
                let lookupValuesFound=$lookupValuesFound+1
      process_a_directory
   done
}
#
#  Command Line Parsing
#
export fileCntr=0
export lookupFile=${FP_ENV}/CDG_Lookup.txt
export cdgFileList=$FP_FTP_DOWN/cdg_loaded_files.txt   # list of files already loaded.
#  remove this next line after testing is done
#
# export FP_CDG_CFR=/fpapps/op/qa/ftproot/cdgtest
#
if [ $# -ne 1 ]
then
   usage_msg
fi
while getopts "k:" option
   do
   case  "$option" in
   k) keyValue="$OPTARG"
      ;;
   [?])
      usage_msg
      ;;
   esac
   done
#
#
if [ "$keyValue" = "" ]
then
        exit_func
fi
#
find_and_load_new_files
#
if [ $lookupValuesFound -eq 0 ]
then
        echo "\n---> ERROR: No values found in lookup file. "
        rc=30
else
        echo "\n---> $lookupValuesFound lookup values found"
fi
#
echo "\n---> Total Files processed is $fileCntr \n"
exit_func

# 4  
Quote:
Originally Posted by brinjit
Hi,

I made the necessary changes but still not able to go beyond the exit function:

Code:
 
#!/usr/bin/ksh
# Note 1
#########################################################################
#  script name: cdg_file_load_test.ksh
#  Created    : July 25, 2013
#  Author     : Brinjit Velu
#
#  Find new files for the file type and period that is passed in in the form
#       of a lookup key that is used to determine what directory and file masks
#       to look for. The lookup file is $FP_ENV/CDG_Lookup.txt
#
#  Revision History: (include date, username, and brief description in
#     descending order)
#########################################################################
#
#  Function definitions
 ... ... ...
#
#  Exit Function
#
function exit_func {
   echo "---> Running exit function..."
         if [ $? -ne 0 ] # Note 2
              then 
                                  update_job_table
         
         fi
        
   exit $rc
         #  update_job_table only when $rc != 0
}
 ... ... ...
#
#   Check for files in a directory
#
#   More like, process a file mask for a directory for a workflow. Several file types
#        can be found in each directory, but must be processed by a different Informatica
#        workflow.
#
function process_a_directory {
        echo "Checking ${FP_CDG_CFR}${searchPath}/${searchMask}"
#       for sourceFile in `ls -1 ${FP_CDG_CFR}${searchPath}/${searchMask}*`
        for sourceFile in `find ${FP_CDG_CFR}${searchPath} -name "${searchMask}" -type f -mtime -5`
        do
                echo "---> Checking $sourceFile "
                has_file_been_loaded
                if [ ${alreadyLoaded} -eq 0 ]
                then
                        function update_job_table { # Note 3
                        # for Jeff to add later  update_job_table
                        load_file
                        let fileCntr=$fileCntr+1
                        echo $sourceFile >> ${cdgFileList} # So we won't load it again
                        #  for Jeff to add later update_job_table 
                }
                fi
        done
}
 ... ... ...
#
#  Command Line Parsing
#
export fileCntr=0
export lookupFile=${FP_ENV}/CDG_Lookup.txt
export cdgFileList=$FP_FTP_DOWN/cdg_loaded_files.txt   # list of files already loaded.
#  remove this next line after testing is done
#
# export FP_CDG_CFR=/fpapps/op/qa/ftproot/cdgtest
#
if [ $# -ne 1 ] # Note 4
then
   usage_msg
fi
while getopts "k:" option
   do
   case  "$option" in
   k) keyValue="$OPTARG"
      ;;
   [?])
      usage_msg
      ;;
   esac
   done
#
#
if [ "$keyValue" = "" ]
then
        exit_func
fi
#
find_and_load_new_files
#
if [ $lookupValuesFound -eq 0 ]
then
        echo "\n---> ERROR: No values found in lookup file. "
        rc=30
else
        echo "\n---> $lookupValuesFound lookup values found"
fi
#
echo "\n---> Total Files processed is $fileCntr \n"
exit_func

I have marked several bits of your code in red where I think changes are needed. I also replaced several sections of your code where I have no comments with ... ... ... to shorten this post. The following notes are connected to the comments I added to your code in blue:
  1. Since #!/usr/bin/ksh is the 2nd line of your script (unless you added an empty line when you added CODE tags), it is just a comment and will not tell your system to use /usr/bin/ksh to run this script. For #! to have any effect on they way your script is run, those two characters must be the 1st two characters on the 1st line in your script.
  2. Why are you checking the value of $? when the following comment in your code says that the function should be called if $rc is not equal to zero? The likelihood that echo will return a non-zero exit status is EXTREMELY low, so it is extremely unlikely that your function will ever be called. The variable rc, however is set by several of the other functions in this script when an error is detected.
  3. This function will not always be defined. Why is this function definition hidden inside an if statement instead of being defined like all of the other functions in this script with a few comment lines before it describing what the function does? Furthermore, your function definition is missing the closing } so this script has a syntax error. Is the only thing you want to do at this spot define a function, or are the comments here just a note that you need to add a function and call it under certain circumstances. With a name like update_job_table, one would expect that this function would add entries to and/or remove entries from a table containing active jobs. The function definition provided here doesn't seem to do anything at all like that.
  4. Shell application command line parsing is a pet peeve of mine. If I am reading your code correctly, the only way to successfully invoke this code is cdg_file_load_test.ksh -kvalue_to_assign_to_keyValue. Standard utility parsing convention allow, but do not require, options with option arguments to be presented as a single argument or as two arguments. (And, strictly conforming applications invoking standard utilities are required to use separate arguments.) So:
    • Why don't you want to allow getopts to do its job and accept both forms?
    • I note that your requirements included:
      Quote:
      The only command line option for the script is –k<key>, where key is the key value from the CDG_Lookup.txt file (such as CYCLE04).

      A sample command line for Control-M would look like this:

      batchjob OP %%JOBNAME ${FP_SCRIPTS}/cdg/cdg_file_load.ksh –eCYCLE04
      You might want to ask the people who wrote these requirements why a script with exactly one "option", requires that option to be present, and does not have any operands would have an option at all; why not just have the keyValue to be used be the one and only operand for this script? (Also point out that their requirement is for a -k option, but their sample invocation of the script uses a -e option instead of a -k option???)
Hope this helps.

On a personal note, taking someone else's code, making modifications to it and replacing their name without even acknowledging their work in the Revision History comments is somewhere between rude and illegal (depending on copyright for the original work and your employer's rules on attributions in code).
# 5  
Changed the code but files moving to another directory

Hi,

I have changed the code but the files are moving to directory where the script is placed. The destination folder is supposed to be /fpapps/op/dev/ftproot/ftpdown, and this is where I want all CSV files to be moved to.

Thanks

Brinjit

The changed code is as follows
Code:
#########################################################################################################################
#
#
#
#      CDG Cycle, EOM, and Daily file transfer Automation
#      Written by Howard Pearl
#
#      Usage:  Called by Control-M, can be called manually as needed
#      ./cdg_transfer.sh BillCycle
#      where BillCycle = 04th, 06th, 07th, 10th, 13th, 19th, 25th, EOM, Weekly, or Daily
#      Backward time functionality added 01/14/2011 By Howard Pearl
#      Usage:  Called via command line
#      ./cdg_transfer.sh BillCycle back
#      where BillCycle = 04th, 06th, 07th, 10th, 13th, 19th, 25th, EOM, Weekly, or Daily
#      input required information at prompts
#
#      Updated 03/18/2011 by Matt Blake - Added code to remove "Processed_" prefix in front of
#      AC0788J files (See Lines 102 - 108).
#
#      Updated 05/10/2011 by Matt Blake - Added code to skip Daily Trigger File generation on
#      on Saturday.
#
#      Updated 06/06/2011 by Matt Blake/Anand Jain - Added code to remove null chars in AC0766
#                  weekly file. Added company code 0584 to
#                  Cycle 7 download.
#      Updated 06/13/2011 by Matt Blake - Added remove null chars to AC0020 and AC0025 weekly files.
#      Updated 06/14/2011 by Matt Blake - Added remove null chars to AC0020, AC0025 and AC0766 EOM files.
#      Updated 06/22/2011 by Matt Blake - Removed 0573 from Cycle19.
#      Updated 06/23/2011 by Matt Blake - Added remove null chars to AC0020, AC0025 and AC0766 Cycle files.
#      Updated 08/11/2011 by Matt Blake - Added remove null chars to AC0780 Cycle files.
#      Updated 12/05/2011 by Matt Blake - CO codes 576, 577, &578 discontinued by CDG
#                 thus removed from the list for Cycle 19
#      Updated 01/18/2012 by Matt Blake - CO codes 574 and 577 removed from all Cycles and removed
#                 576 and 578 from Cycle 10 per Kurt Trygg
#      Updated 02/07/2012 by Matt Blake - Added chmod -R 777 $filedir to end of each cycle function
#               to ensure cycle folders can be manipulated.  For the EOM
#               function added chmod -R 777 $filepath since $filedir not used.
#      Updated 04/02/2012 by Matt Blake - Added CASE statement in CycleEOM Function to check if previous
#               month is between 1 and 9, then concats a "0" to fit correct
#               folder creation.
#      Updated 04/11/2012 by Matt Blake - Added CASE statement in the main function to check if jday
#               is between 1 and 9, then concats a "0" to fit correct
#               folder creation.
#      Updated 06/18/2012 by Matt Blake - Added files AC0029 and AC0766 to daily file filter.
#      Updated 07/05/2012 by Matt Blake - Added files AC0015, AC0027, AC0093 to daily file filter.
#
#      Updated 07/06/2012 by Matt Blake - Added files AC0020 from 0581, 0582, 0583, 0587 along with
#               AC0025, AC0047 and AC0650 files to weekly file transfer.
#               Added AC0011 and ASPL.ACKU files to daily file transfer.
#      Updated 10/30/2012 by Matt Blake - Added file AC0025 to daily file filter.
#      Updated 11/16/2012 by Matt Blake - Setup script to not create a Trigger File for the Weekly downloads
#      Updated 11/28/2012 by Matt Blake - Added email for showing what files are being downloaded for the day
################################################################################################################################
#
#
#Main Variable declarations
#
#
#######################################################################################
HOST='172.24.80.90'
#CDG USER NAME AND PASSWORD
USER='opdev'
PASS='opdev890!'
function setdates
{
#function called if no dates are specified in backwards dating
curdate="`date +%Y%m`"
curyear="`date +%Y`"
curdateday="`date +%Y%m%d`"
curmonth="`date +%m`"
curday="`date +%d`"
prevday="`expr $curday - 1`"
curdow="`date +%A`"  # Added for Saturday daily trigger file check.
}
fancydate="`date +%A,` `date +%B ` `date +%d ` `date +%Y`"
curdate2="`date +%Y%m`"
curyear2="`date +%Y`"
curdateday2="`date +%Y%m%d`"
curmonth2="`date +%m`"
curday2="`date +%d`"
curdow2="`date +%A`"   # Added for Saturday daily trigger file check.
billcycle=$1
lastdir=0
nofiles=" "
rtncode=0
filename=""
files=""
counter=0
finaldir='/fpapps/op/dev/ftproot/ftpdown/'
FILE=localftpout
#####################################################################################
#
#File transfer function for cycle and EOM files.
#Weekly and daily transfers finalized 01/14/2011
#
#####################################################################################
function transfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $remotedir
   mget *
  bye
EOF
}
function AC0877aTrans
{
cd $1
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd /fpapps/cfr/CDG/Reports
   echo $curmonth
   mget *0877A.$curmonth''*
  bye
EOF
AC0877jTrans $1 'EOM' $2
}
function AC0877jTrans
{
cd $1
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd /fpapps/cfr/CDG/Reports
   mget *0877J.D$3*
  bye
EOF
for origfile_name in `ls Processed_*AC0877*.CSV`
do
 # awk command to get the correct file name by removing "Processed" as prefix
 renamefile_name=`echo $origfile_name | awk -F\_ '{ print $2 }' | sed -e 's/_//'`
 # move the processed file to regular file name
 mv $origfile_name $renamefile_name
done
case $2 in
 Cycle) CycleTrigger ;;
 EOM) EOMTrigger ;;
 *) exit 99 ;;
 esac
}
#Added 01/11/2010 to support transferring daily files
function dailytransfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $1
   mget $2
  bye
EOF
#######Code to Replace Special Char in AC0766 and AC0025:Begin#######
     if [[ $2 = *AC0766*.CSV || $2 = *AC0025*.CSV ]] then
        tr '\000' '\ '<$2>$2.tmp
        mv $2.tmp $2
     fi
#######Code to Replace Special Char in AC0766, AC0020 and AC0025:End#######
}
function getftpfilenames
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   dir $remotedir localftpout
   dir $remotedir2 localftpout2
  bye
EOF
}
#reads filenames and matches on today's date and prefix of "AC"
function dailyreadftpfilenames
{
cat localftpout | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir' '$NINE " " >> ftpget
  fi
 fi
 if [[ $SEVEN -eq $prevday ]]
 then
  if [[ $NINE = *'AC0025'* ]]    #Added because AC0025 daily date is always current day - 1
  then
   echo $remotedir' '$NINE " " >> ftpgetAC25
  fi
 fi
done
cat ftpgetAC25 >> ftpget  ##Append AC0025 File name to ftpget list
cat localftpout2 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpget
  fi
 fi
 if [[ $SEVEN -eq $prevday ]]
 then
  if [[ $NINE = *'ASPL.ACKU'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpgetASPL
  fi
 fi
done
cat ftpgetASPL >> ftpget
rm ftpgetAC25
rm ftpgetASPL
}
#Filters files to only get names contained below
function dailyfilefilter
{
 remotedir='/fpapps/cfr/CDG/Reports/0570'
 remotedir2='/fpapps/cfr/CDG/Reports'
 for filenames in `cat ftpget`
 do
  case $filenames in
  *ASPL.ACKU*) echo $filenames >> email_body_daily.txt ;;
  *AC0011*) echo $filenames >> email_body_daily.txt ;;
  *AC0015*) echo $filenames >> email_body_daily.txt ;;
  *AC0027*) echo $filenames >> email_body_daily.txt ;;
  *AC0025*) echo $filenames >> email_body_daily.txt ;;
  *AC0029*) echo $filenames >> email_body_daily.txt ;;
  *AC0045*) echo $filenames >> email_body_daily.txt ;;
  *AC0048*) echo $filenames >> email_body_daily.txt ;;
  *AC0065*) echo $filenames >> email_body_daily.txt ;;
  *AC0093*) echo $filenames >> email_body_daily.txt ;;
  *AC0110*) echo $filenames >> email_body_daily.txt ;;
  *AC0766*) echo $filenames >> email_body_daily.txt ;;
  *AC0783*) echo $filenames >> email_body_daily.txt ;;
  esac
 done
 cat email_body_daily.txt | mailx -s "CDG Files Downloading Today, $fancydate" cdgtomdasuccess@fairpoint.com
 rm email_body_daily.txt
 for filenames in `cat ftpget`
 do
  case $filenames in
  *ASPL.ACKU*) dailytransfiles $remotedir2 $filenames ;;
  *AC0011*) dailytransfiles $remotedir2 $filenames ;;
  *AC0015*) dailytransfiles $remotedir $filenames ;;
  *AC0027*) dailytransfiles $remotedir $filenames ;;
  *AC0025*) dailytransfiles $remotedir $filenames ;;
  *AC0029*) dailytransfiles $remotedir $filenames ;;
  *AC0045*) dailytransfiles $remotedir $filenames ;;
  *AC0048*) dailytransfiles $remotedir $filenames ;;
  *AC0065*) dailytransfiles $remotedir $filenames ;;
  *AC0093*) dailytransfiles $remotedir $filenames ;;
  *AC0110*) dailytransfiles $remotedir $filenames ;;
  *AC0766*) dailytransfiles $remotedir $filenames ;;
  *AC0783*) dailytransfiles $remotedir $filenames ;;
  esac
 done
}
function getftpfilenamesweekly
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   dir $remotedir localftpout
   dir $remotedir2 localftpout2
   dir $remotedir3 localftpout3
   dir $remotedir4 localftpout4
   dir $remotedir5 localftpout5
  bye
EOF
}
function weeklyreadftpfilenames
{
cat localftpout | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout2 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout3 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir3' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout4 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir4' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout5 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir5' '$NINE " " >> ftpget
  fi
 fi
done
rm localftpout
rm localftpout2
rm localftpout3
rm localftpout4
rm localftpout5
}
function weeklyfilefilter
{
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir3='/fpapps/cfr/CDG/Reports/0582'
remotedir4='/fpapps/cfr/CDG/Reports/0583'
remotedir5='/fpapps/cfr/CDG/Reports/0587'
 for filenames in `cat ftpget`
 do
  case $filenames in
  *C0570*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0581*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0582*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0583*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0587*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *AC0025*) echo $filenames >> email_body_weekly.txt ;;
  *AC0047*) echo $filenames >> email_body_weekly.txt ;;
  *AC0650*) echo $filenames >> email_body_weekly.txt ;;
  esac
 done
 cat email_body_weekly.txt | mailx -s "CDG Files Downloading Today, $fancydate" cdgtomdasuccess@fairpoint.com
 rm email_body_weekly.txt
 for filenames in `cat ftpget`
 do
  case $filenames in
  *C0570*AC0020*) weeklytransfiles $remotedir $filenames ;;
  *C0581*AC0020*) weeklytransfiles $remotedir2 $filenames ;;
  *C0582*AC0020*) weeklytransfiles $remotedir3 $filenames ;;
  *C0583*AC0020*) weeklytransfiles $remotedir4 $filenames ;;
  *C0587*AC0020*) weeklytransfiles $remotedir5 $filenames ;;
  *AC0025*) weeklytransfiles $remotedir $filenames ;;
  *AC0047*) weeklytransfiles $remotedir $filenames ;;
  *AC0650*) weeklytransfiles $remotedir $filenames ;;
  esac
 done
 rm ftpget
}
function weeklytransfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $1
   mget $2
  bye
EOF
#######Code to Replace Special Char in AC0650, AC0020, AC0047 and AC0025:Begin#######
     if [[ $2 = *AC0650*.CSV || $2 = *AC0020*.CSV || $2 = *AC0025*.CSV || $2 = *AC0047*.CSV ]] then
        tr '\000' '\ '<$2>$2.tmp
        mv $2.tmp $2
     fi
#######Code to Replace Special Char in AC0650, AC0020, AC0047 and AC0025:End#######
}
#####################################################################################
#
#Remove Empty Cycle Files if no file is present
#   added 01/06/2011
#
#####################################################################################
function checkMissingCycleFiles
{
cd $1
files=$(ls $1/* 2> /dev/null  wc -l)
if [ -z $files ]
 then
  cd ..
  rm -r $3'_'$4
  directories=$(ls $2/* 2> /dev/null  wc -l)
  if [ -z $directories ]
  then
   cd ..
   rm -r $4
  fi
 cd $finaldir
 echo "No files in " $5 "\n" >> testfile
 rtncode=6
 fi
return $rtncode
}
#####################################################################################
#
#Individual Cycle functions
#
#####################################################################################
function cycle4
{
while [ $counter -lt 6 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 4) lastdir=0582 ;;
 5) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle6
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0572 ;;
 1) lastdir=0581 ;;
 2) lastdir=0582 ;;
 3) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle7
{
while [ $counter -lt 5 ]         #Updated 06/06/2011 to download company code 0584
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 4) lastdir=0584 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle10
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle13
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle19
{
while [ $counter -lt 6 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 4) lastdir=0582 ;;
 5) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle25
{
while [ $counter -lt 10 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 4) lastdir=0576 ;;
 5) lastdir=0578 ;;
 6) lastdir=0581 ;;
 7) lastdir=0582 ;;
 8) lastdir=0583 ;;
 9) lastdir=0587 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycleDaily
{
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/Daily_reports_'$curdate
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports'
if [[ -d $filepath ]]
then
 cd $filepath
 getftpfilenames
 dailyreadftpfilenames
 dailyfilefilter
 chmod -R 777 $filepath
else
 mkdir -p $filepath
 cd $filepath
 getftpfilenames
 dailyreadftpfilenames
 dailyfilefilter
 chmod -R 777 $filepath
fi
cd $filepath
rm ftpget
rm localftpout
rm localftpout2
}
function cycleWeekly
{
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/Daily_reports_'$curdate
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir3='/fpapps/cfr/CDG/Reports/0582'
remotedir4='/fpapps/cfr/CDG/Reports/0583'
remotedir5='/fpapps/cfr/CDG/Reports/0587'
if [[ -d $filepath ]]
then
 cd $filepath
 getftpfilenamesweekly
 weeklyreadftpfilenames
 weeklyfilefilter
 chmod -R 777 $filepath
else
 mkdir -p $filepath
 cd $filepath
 getftpfilenamesweekly
 weeklyreadftpfilenames
 weeklyfilefilter
 chmod -R 777 $filepath
fi
}
function cycleEOM
{
if [[ $curday -le 15 ]]
then
 month=$curmonth
 year=$curyear
 lmonth=`expr $month - 1`
 if test "$lmonth" = "0"
  then
  lmonth=12
  year=`expr $year - 1`
 fi
 #checks concats lmonths between 1 and 9 with a 0 to form correct folders.
 case $lmonth in
  "1") lmonth="0"$lmonth;;
  "2") lmonth="0"$lmonth;;
  "3") lmonth="0"$lmonth;;
  "4") lmonth="0"$lmonth;;
  "5") lmonth="0"$lmonth;;
  "6") lmonth="0"$lmonth;;
  "7") lmonth="0"$lmonth;;
  "8") lmonth="0"$lmonth;;
  "9") lmonth="0"$lmonth;;
  *) lmonth=$lmonth;;
  esac
 curdate=$year$lmonth
 curyear=$year
 curmonth=$lmonth
 #Used for if EOM cycle runs after the end of the month. I.E. December's transfer running after the 31st.
fi
while [ $counter -lt 5 ]
do
case $counter in
 0) lastdir=0570
   billcycle='EOM_FILES'
   ;;
 1) lastdir=0581
   billcycle='UNE_EOM'
   ;;
 2) lastdir=0582
   billcycle='582_EOM'
   ;;
 3) lastdir=0583
   billcycle='583_EOM'
   ;;
 4) lastdir=0587
   billcycle='UNE_L_EOM'
   ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/EOM_reports_'$curdate'/'
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for eomfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $eomfnm = *AC0766*.CSV  || $eomfnm = *AC0020*.CSV || $eomfnm = *AC0025*.CSV || $eomfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$eomfnm>$eomfnm.tmp
   mv $eomfnm.tmp $eomfnm
   chmod 775 $eomfnm
   fi
 done
 counter=`expr $counter + 1`
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for eomfnm in `ls $filepath/**`   ##After files transferred, remove null chars
 do
  if [[ $eomfnm = *AC0766*.CSV  || $eomfnm = *AC0020*.CSV || $eomfnm = *AC0025*.CSV || $eomfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$eomfnm>$eomfnm.tmp
   mv $eomfnm.tmp $eomfnm
   chmod 775 $eomfnm
   fi
 done
 counter=`expr $counter + 1`
files=$(ls $filepath/* 2> /dev/null  wc -l)
 if [ -z $files ]
 then
  cd ..
  rm -r EOM_reports_$curdate
  echo "No files in " $remotedir "\n" >> testfile
  rtncode=6
 fi
fi
done
cd $filepath
chmod -R 777 $filepath
}
###########################################################################
#
#
#      Trigger Fyle generation
#     Written: 01/05/2011 by Howard Pearl
#      For Project Sophia
#     Updated 01/21/2011, changed trigger file location per Raj by Howard Pearl
#
#
############################################################################
function CycleTrigger
{
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
 if [[ -d $triggerpath ]]
 then
  cd $triggerpath
  touch 'CYCLE.'$curdate''$billcycle'.OK'
 else
  mkdir -p $triggerpath
  cd $triggerpath
  touch 'CYCLE.'$curdate''$billcycle'.OK'
 fi
}
function EOMTrigger
{
if [[ $curday -le 15 ]]
then
 month=$curmonth2
 year=$curyear2
 lmonth=`expr $month - 1`
 if test "$lmonth" = "0"
  then
  lmonth=12
  year=`expr $year - 1`
 fi
 curdate=$year$lmonth
 curyear=$year
 #Used for if EOM cycle runs after the end of the month. I.E. December's transfer running after the 31st.
fi
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
 if [[ -d $triggerpath ]]
 then
  cd $triggerpath
  touch 'EOM.'$curdate'.OK'
 else
  mkdir -p $triggerpath
  cd $triggerpath
  touch 'EOM.'$curdate'.OK'
 fi
}
#Also used for weekly trigger files Per Raj (01/17/2011)
function DailyTrigger
{
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
if [[ $curdow != Saturday ]]  #If current day is not Saturday, then the trigger file is created.
 then
  if [[ $curdow != Sunday ]] #If current day is not Sunday, then the trigger file is created.
   then
   if [[ -d $triggerpath ]]
    then
    cd $triggerpath
    touch 'DAILY.'$curdateday'.OK'
   else
    mkdir -p $triggerpath
    cd $triggerpath
    touch 'DAILY.'$curdateday'.OK'
   fi
  fi
 fi
}
#########################################################################
#
#     Used for reseting dates if run for older date
#
#########################################################################
function resetdates
{
  read old_day?"Enter Day as Number: "
 case $old_day in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 case $old_day in
     1) newday=01 ;;
     2) newday=02 ;;
     3) newday=03 ;;
     4) newday=04 ;;
     5) newday=05 ;;
     6) newday=06 ;;
     7) newday=07 ;;
     8) newday=08 ;;
     9) newday=09 ;;
     *) newday=$old_day ;;
     esac
 read old_month?"Enter Month as Number: "
 case $old_month in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 case $old_month in
   1) newmonth=01 ;;
   2) newmonth=02 ;;
   3) newmonth=03 ;;
   4) newmonth=04 ;;
   5) newmonth=05 ;;
   6) newmonth=06 ;;
   7) newmonth=07 ;;
   8) newmonth=08 ;;
   9) newmonth=09 ;;
   01) newmonth=01 ;;
   02) newmonth=02 ;;
   03) newmonth=03 ;;
   04) newmonth=04 ;;
   05) newmonth=05 ;;
   06) newmonth=06 ;;
   07) newmonth=07 ;;
   08) newmonth=08 ;;
   09) newmonth=09 ;;
   10) newmonth=10 ;;
   11) newmonth=11 ;;
   12) newmonth=12 ;;
   *) echo "There are only 12 months in a year"
    exit 99  ;;
   esac
 read old_year?"Enter Year as four digit number: "
 case $old_year in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 len=${#old_year}
 if [[ $len -eq 4 ]]
 then
  newyear=$old_year
 else
  echo "Year must be 4 digits"
  exit 99
 fi

 curdate=$old_year''$old_month
 curyear=$old_year
 curdateday=$old_year''$old_month''$old_day
 curmonth=$old_month
 curday=$old_day
}
#########################################################################
#
#
#
#      Main Function Begins Here
#
#
#
#########################################################################
#determines if past date needs to be specified based on command line inputs *see usage note above*
if [[ $2 = 'old' ]]
then
 resetdates
else
 setdates
fi
case $billcycle in
 04th) cycle4 ;;
 06th) cycle6 ;;
 07th) cycle7 ;;
 10th) cycle10 ;;
 13th) cycle13 ;;
 19th) cycle19 ;;
 25th) cycle25 ;;
 Daily) cycleDaily ;;
 EOM) cycleEOM ;;
 Weekly) cycleWeekly ;;
 *) exit 99 ;;
 esac
#if return code is 6, no files available for given cycle.  See system output in Control-M for details as to which files are missing
if [[ $rtncode -eq 6 ]]
then
 cd $finaldir
 cat testfile
 rm -r testfile
 exit 6
else
#Minus one day for 877J transfer
 filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'
 eomfilepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'
 day=$curday2
 jmonth=$curmonth2
 jday=`expr $day - 1`
 if [[ $jday -eq 0 ]]
  then
  jmonth=`expr $jmonth - 1`
  if [[ $jmonth -eq 0 ]]
  then
   jmonth=12
  fi
  case $jmonth in
   1) jday=31 ;;
   2) jday=28 ;;
   3) jday=31 ;;
   4) jday=30 ;;
   5) jday=31 ;;
   6) jday=30 ;;
   7) jday=31 ;;
   8) jday=31 ;;
   9) jday=30 ;;
   10) jday=31 ;;
   11) jday=30 ;;
   12) jday=31 ;;
   *) exit 99 ;;
   esac
 fi
 #checks and concats jday between 1 and 9 with a 0 to form correct folders.
 case $jday in
  "1") jday="0"$jday;;
  "2") jday="0"$jday;;
  "3") jday="0"$jday;;
  "4") jday="0"$jday;;
  "5") jday="0"$jday;;
  "6") jday="0"$jday;;
  "7") jday="0"$jday;;
  "8") jday="0"$jday;;
  "9") jday="0"$jday;;
  *) jday=$jday;;
  esac
 acjdate=$jmonth''$jday
#runs 877j trans for cycle files, and 877j and 877a files for monthly files
#Weekly trigger information added 01/17/2011 by Howard Pearl
echo $billcycle
 case $billcycle in
 04th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 06th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 07th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 10th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 13th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 19th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 25th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 Daily) DailyTrigger ;;
 Weekly) DailyTrigger ;;
 EOM) AC0877aTrans $eomfilepath $acjdate ;;
 *) exit 99 ;;
 esac
 exit 0
fi

# 6  
So what have you done to debug your script?
  1. Have you executed your script with tracing (set -xv) turned on?
  2. You have 19 mv commands in your script:
    1. What have you done to figure out which one(s) is (are) "broken"?
    2. For any mv command that isn't doing what you want it to, how are the variables set that are passed as parameters to that mv command? How should they have been set to have the files be mored to the directory where you wanted them to be moved?
  3. You have dozen of cd commands in your script. Have you verified that any mv command that is not working correctly is invoked while sitting in the directory where you expected it to be invoked? If not, should the script change to the directory you expected, or should the operands to the mv command be altered to set the correct destination directory?
# 7  
Unable to get only the required files in the target directory

Hi,

What is really happening is that even though I have changed the criteria for finding masked files to cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*, I still am getting .txt and .pdf from the source directory and all are being populated into the target

The modified code looks something like this now:

Code:
 
#########################################################################################################################
#
#
#
#      CDG Cycle, EOM, and Daily file transfer Automation
#      Written by Howard Pearl
#
#      Usage:  Called by Control-M, can be called manually as needed
#      ./cdg_transfer.sh BillCycle
#      where BillCycle = 04th, 06th, 07th, 10th, 13th, 19th, 25th, EOM, Weekly, or Daily
#      Backward time functionality added 01/14/2011 By Howard Pearl
#      Usage:  Called via command line
#      ./cdg_transfer.sh BillCycle back
#      where BillCycle = 04th, 06th, 07th, 10th, 13th, 19th, 25th, EOM, Weekly, or Daily
#      input required information at prompts
#
#      Updated 03/18/2011 by Matt Blake - Added code to remove "Processed_" prefix in front of
#      AC0788J files (See Lines 102 - 108).
#
#      Updated 05/10/2011 by Matt Blake - Added code to skip Daily Trigger File generation on
#      on Saturday.
#
#      Updated 06/06/2011 by Matt Blake/Anand Jain - Added code to remove null chars in AC0766
#                  weekly file. Added company code 0584 to
#                  Cycle 7 download.
#      Updated 06/13/2011 by Matt Blake - Added remove null chars to AC0020 and AC0025 weekly files.
#      Updated 06/14/2011 by Matt Blake - Added remove null chars to AC0020, AC0025 and AC0766 EOM files.
#      Updated 06/22/2011 by Matt Blake - Removed 0573 from Cycle19.
#      Updated 06/23/2011 by Matt Blake - Added remove null chars to AC0020, AC0025 and AC0766 Cycle files.
#      Updated 08/11/2011 by Matt Blake - Added remove null chars to AC0780 Cycle files.
#      Updated 12/05/2011 by Matt Blake - CO codes 576, 577, &578 discontinued by CDG
#                 thus removed from the list for Cycle 19
#      Updated 01/18/2012 by Matt Blake - CO codes 574 and 577 removed from all Cycles and removed
#                 576 and 578 from Cycle 10 per Kurt Trygg
#      Updated 02/07/2012 by Matt Blake - Added chmod -R 777 $filedir to end of each cycle function
#               to ensure cycle folders can be manipulated.  For the EOM
#               function added chmod -R 777 $filepath since $filedir not used.
#      Updated 04/02/2012 by Matt Blake - Added CASE statement in CycleEOM Function to check if previous
#               month is between 1 and 9, then concats a "0" to fit correct
#               folder creation.
#      Updated 04/11/2012 by Matt Blake - Added CASE statement in the main function to check if jday
#               is between 1 and 9, then concats a "0" to fit correct
#               folder creation.
#      Updated 06/18/2012 by Matt Blake - Added files AC0029 and AC0766 to daily file filter.
#      Updated 07/05/2012 by Matt Blake - Added files AC0015, AC0027, AC0093 to daily file filter.
#
#      Updated 07/06/2012 by Matt Blake - Added files AC0020 from 0581, 0582, 0583, 0587 along with
#               AC0025, AC0047 and AC0650 files to weekly file transfer.
#               Added AC0011 and ASPL.ACKU files to daily file transfer.
#      Updated 10/30/2012 by Matt Blake - Added file AC0025 to daily file filter.
#      Updated 11/16/2012 by Matt Blake - Setup script to not create a Trigger File for the Weekly downloads
#      Updated 11/28/2012 by Matt Blake - Added email for showing what files are being downloaded for the day
################################################################################################################################
#
#
#Main Variable declarations
#
#
#######################################################################################
HOST='172.24.80.90'
#CDG USER NAME AND PASSWORD
USER='opdev'
PASS='opdev890!'
function setdates
{
#function called if no dates are specified in backwards dating
curdate="`date +%Y%m`"
curyear="`date +%Y`"
curdateday="`date +%Y%m%d`"
curmonth="`date +%m`"
curday="`date +%d`"
prevday="`expr $curday - 1`"
curdow="`date +%A`"  # Added for Saturday daily trigger file check.
}
fancydate="`date +%A,` `date +%B ` `date +%d ` `date +%Y`"
curdate2="`date +%Y%m`"
curyear2="`date +%Y`"
curdateday2="`date +%Y%m%d`"
curmonth2="`date +%m`"
curday2="`date +%d`"
curdow2="`date +%A`"   # Added for Saturday daily trigger file check.
billcycle=$1
lastdir=0
nofiles=" "
rtncode=0
filename=""
files=""
counter=0
finaldir='/fpapps/op/dev/ftproot/ftpdown/'
FILE=localftpout
#####################################################################################
#
#File transfer function for cycle and EOM files.
#Weekly and daily transfers finalized 01/14/2011
#
#####################################################################################
function transfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $remotedir
   mget *
  bye
EOF
}
function AC0877aTrans
{
cd $1
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd /fpapps/cfr/CDG/Reports
   echo $curmonth
   mget *0877A.$curmonth''*
  bye
EOF
AC0877jTrans $1 'EOM' $2
}
function AC0877jTrans
{
cd $1
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd /fpapps/cfr/CDG/Reports
   mget *0877J.D$3*
  bye
EOF
for origfile_name in `ls Processed_*AC0877*`
do
 # awk command to get the correct file name by removing "Processed" as prefix
 renamefile_name=`echo $origfile_name | awk -F\_ '{ print $2 }' | sed -e 's/_//'`
 # move the processed file to regular file name
 mv $origfile_name $finaldir/$renamefile_name
done
case $2 in
 Cycle) CycleTrigger ;;
 EOM) EOMTrigger ;;
 *) exit 99 ;;
 esac
}
#Added 01/11/2010 to support transferring daily files
function dailytransfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $1
   mget $2
  bye
EOF
#######Code to Replace Special Char in AC0766 and AC0025:Begin#######
     if [[ $2 = *AC0766*.CSV || $2 = *AC0025*.CSV ]] then
        tr '\000' '\ '<$2>$2.tmp
        mv $2.tmp $finaldir/$2
     fi
#######Code to Replace Special Char in AC0766, AC0020 and AC0025:End#######
}
function getftpfilenames
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   dir $remotedir localftpout
   dir $remotedir2 localftpout2
  bye
EOF
}
#reads filenames and matches on today's date and prefix of "AC"
function dailyreadftpfilenames
{
cat localftpout | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir' '$NINE " " >> ftpget
  fi
 fi
 if [[ $SEVEN -eq $prevday ]]
 then
  if [[ $NINE = *'AC0025'* ]]    #Added because AC0025 daily date is always current day - 1
  then
   echo $remotedir' '$NINE " " >> ftpgetAC25
  fi
 fi
done
cat ftpgetAC25 >> ftpget  ##Append AC0025 File name to ftpget list
cat localftpout2 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpget
  fi
 fi
 if [[ $SEVEN -eq $prevday ]]
 then
  if [[ $NINE = *'ASPL.ACKU'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpgetASPL
  fi
 fi
done
cat ftpgetASPL >> ftpget
rm ftpgetAC25
rm ftpgetASPL
}
#Filters files to only get names contained below
function dailyfilefilter
{
 remotedir='/fpapps/cfr/CDG/Reports/0570'
 remotedir2='/fpapps/cfr/CDG/Reports'
 for filenames in `cat ftpget`
 do
  case $filenames in
  *ASPL.ACKU*) echo $filenames >> email_body_daily.txt ;;
  *AC0011*) echo $filenames >> email_body_daily.txt ;;
  *AC0015*) echo $filenames >> email_body_daily.txt ;;
  *AC0027*) echo $filenames >> email_body_daily.txt ;;
  *AC0025*) echo $filenames >> email_body_daily.txt ;;
  *AC0029*) echo $filenames >> email_body_daily.txt ;;
  *AC0045*) echo $filenames >> email_body_daily.txt ;;
  *AC0048*) echo $filenames >> email_body_daily.txt ;;
  *AC0065*) echo $filenames >> email_body_daily.txt ;;
  *AC0093*) echo $filenames >> email_body_daily.txt ;;
  *AC0110*) echo $filenames >> email_body_daily.txt ;;
  *AC0766*) echo $filenames >> email_body_daily.txt ;;
  *AC0783*) echo $filenames >> email_body_daily.txt ;;
  esac
 done
 cat email_body_daily.txt | mailx -s "CDG Files Downloading Today, $fancydate" cdgtomdasuccess@fairpoint.com
 rm email_body_daily.txt
 for filenames in `cat ftpget`
 do
  case $filenames in
  *ASPL.ACKU*) dailytransfiles $remotedir2 $filenames ;;
  *AC0011*) dailytransfiles $remotedir2 $filenames ;;
  *AC0015*) dailytransfiles $remotedir $filenames ;;
  *AC0027*) dailytransfiles $remotedir $filenames ;;
  *AC0025*) dailytransfiles $remotedir $filenames ;;
  *AC0029*) dailytransfiles $remotedir $filenames ;;
  *AC0045*) dailytransfiles $remotedir $filenames ;;
  *AC0048*) dailytransfiles $remotedir $filenames ;;
  *AC0065*) dailytransfiles $remotedir $filenames ;;
  *AC0093*) dailytransfiles $remotedir $filenames ;;
  *AC0110*) dailytransfiles $remotedir $filenames ;;
  *AC0766*) dailytransfiles $remotedir $filenames ;;
  *AC0783*) dailytransfiles $remotedir $filenames ;;
  esac
 done
}
function getftpfilenamesweekly
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   dir $remotedir localftpout
   dir $remotedir2 localftpout2
   dir $remotedir3 localftpout3
   dir $remotedir4 localftpout4
   dir $remotedir5 localftpout5
  bye
EOF
}
function weeklyreadftpfilenames
{
cat localftpout | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout2 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir2' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout3 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir3' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout4 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir4' '$NINE " " >> ftpget
  fi
 fi
done
cat localftpout5 | while read ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
do
 if [[ $SEVEN -eq $curday ]]
 then
  if [[ $NINE = 'AC'* ]]
  then
   echo $remotedir5' '$NINE " " >> ftpget
  fi
 fi
done
rm localftpout
rm localftpout2
rm localftpout3
rm localftpout4
rm localftpout5
}
function weeklyfilefilter
{
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir3='/fpapps/cfr/CDG/Reports/0582'
remotedir4='/fpapps/cfr/CDG/Reports/0583'
remotedir5='/fpapps/cfr/CDG/Reports/0587'
 for filenames in `cat ftpget`
 do
  case $filenames in
  *C0570*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0581*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0582*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0583*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *C0587*AC0020*) echo $filenames >> email_body_weekly.txt ;;
  *AC0025*) echo $filenames >> email_body_weekly.txt ;;
  *AC0047*) echo $filenames >> email_body_weekly.txt ;;
  *AC0650*) echo $filenames >> email_body_weekly.txt ;;
  esac
 done
 cat email_body_weekly.txt | mailx -s "CDG Files Downloading Today, $fancydate" cdgtomdasuccess@fairpoint.com
 rm email_body_weekly.txt
 for filenames in `cat ftpget`
 do
  case $filenames in
  *C0570*AC0020*) weeklytransfiles $remotedir $filenames ;;
  *C0581*AC0020*) weeklytransfiles $remotedir2 $filenames ;;
  *C0582*AC0020*) weeklytransfiles $remotedir3 $filenames ;;
  *C0583*AC0020*) weeklytransfiles $remotedir4 $filenames ;;
  *C0587*AC0020*) weeklytransfiles $remotedir5 $filenames ;;
  *AC0025*) weeklytransfiles $remotedir $filenames ;;
  *AC0047*) weeklytransfiles $remotedir $filenames ;;
  *AC0650*) weeklytransfiles $remotedir $filenames ;;
  esac
 done
 rm ftpget
}
function weeklytransfiles
{
ftp -inv $HOST <<EOF
   user $USER $PASS
   passive
   cd $1
   mget $2
  bye
EOF
#######Code to Replace Special Char in AC0650, AC0020, AC0047 and AC0025:Begin#######
     if [[ $2 = *AC0650*.CSV || $2 = *AC0020*.CSV || $2 = *AC0025*.CSV || $2 = *AC0047*.CSV ]] then
        tr '\000' '\ '<$2>$2.tmp
        mv $2.tmp $finaldir/$2
     fi
#######Code to Replace Special Char in AC0650, AC0020, AC0047 and AC0025:End#######
}
#####################################################################################
#
#Remove Empty Cycle Files if no file is present
#   added 01/06/2011
#
#####################################################################################
function checkMissingCycleFiles
{
cd $1
files=$(ls $1/* 2> /dev/null  wc -l)
if [ -z $files ]
 then
  cd ..
  rm -r $3'_'$4
  directories=$(ls $2/* 2> /dev/null  wc -l)
  if [ -z $directories ]
  then
   cd ..
   rm -r $4
  fi
 cd $finaldir
 echo "No files in " $5 "\n" >> testfile
 rtncode=6
 fi
return $rtncode
}
#####################################################################################
#
#Individual Cycle functions
#
#####################################################################################
function cycle4
{
while [ $counter -lt 6 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 4) lastdir=0582 ;;
 5) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle6
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0572 ;;
 1) lastdir=0581 ;;
 2) lastdir=0582 ;;
 3) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle7
{
while [ $counter -lt 5 ]         #Updated 06/06/2011 to download company code 0584
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 4) lastdir=0584 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle10
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle13
{
while [ $counter -lt 4 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle19
{
while [ $counter -lt 6 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0581 ;;
 4) lastdir=0582 ;;
 5) lastdir=0583 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycle25
{
while [ $counter -lt 10 ]
do
case $counter in
 0) lastdir=0570 ;;
 1) lastdir=0571 ;;
 2) lastdir=0572 ;;
 3) lastdir=0573 ;;
 4) lastdir=0576 ;;
 5) lastdir=0578 ;;
 6) lastdir=0581 ;;
 7) lastdir=0582 ;;
 8) lastdir=0583 ;;
 9) lastdir=0587 ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'$lastdir'_'$billcycle
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
filedir='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for cycfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $cycfnm = *AC0766*.CSV  || $cycfnm = *AC0020*.CSV || $cycfnm = *AC0025*.CSV || $cycfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$cycfnm>$cycfnm.tmp
   mv $cycfnm.tmp $finaldir/$cycfnm
   chmod 775 $cycfnm
   fi
 done
 counter=`expr $counter + 1`
 checkMissingCycleFiles $filepath $filedir $lastdir $billcycle $remotedir
fi
chmod -R 777 $filedir
done
}
function cycleDaily
{
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/Daily_reports_'$curdate
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports'
if [[ -d $filepath ]]
then
 cd $filepath
 getftpfilenames
 dailyreadftpfilenames
 dailyfilefilter
 chmod -R 777 $filepath
else
 mkdir -p $filepath
 cd $filepath
 getftpfilenames
 dailyreadftpfilenames
 dailyfilefilter
 chmod -R 777 $filepath
fi
cd $filepath
rm ftpget
rm localftpout
rm localftpout2
}
function cycleWeekly
{
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/Daily_reports_'$curdate
remotedir='/fpapps/cfr/CDG/Reports/0570'
remotedir2='/fpapps/cfr/CDG/Reports/0581'
remotedir3='/fpapps/cfr/CDG/Reports/0582'
remotedir4='/fpapps/cfr/CDG/Reports/0583'
remotedir5='/fpapps/cfr/CDG/Reports/0587'
if [[ -d $filepath ]]
then
 cd $filepath
 getftpfilenamesweekly
 weeklyreadftpfilenames
 weeklyfilefilter
 chmod -R 777 $filepath
else
 mkdir -p $filepath
 cd $filepath
 getftpfilenamesweekly
 weeklyreadftpfilenames
 weeklyfilefilter
 chmod -R 777 $filepath
fi
}
function cycleEOM
{
if [[ $curday -le 15 ]]
then
 month=$curmonth
 year=$curyear
 lmonth=`expr $month - 1`
 if test "$lmonth" = "0"
  then
  lmonth=12
  year=`expr $year - 1`
 fi
 #checks concats lmonths between 1 and 9 with a 0 to form correct folders.
 case $lmonth in
  "1") lmonth="0"$lmonth;;
  "2") lmonth="0"$lmonth;;
  "3") lmonth="0"$lmonth;;
  "4") lmonth="0"$lmonth;;
  "5") lmonth="0"$lmonth;;
  "6") lmonth="0"$lmonth;;
  "7") lmonth="0"$lmonth;;
  "8") lmonth="0"$lmonth;;
  "9") lmonth="0"$lmonth;;
  *) lmonth=$lmonth;;
  esac
 curdate=$year$lmonth
 curyear=$year
 curmonth=$lmonth
 #Used for if EOM cycle runs after the end of the month. I.E. December's transfer running after the 31st.
fi
while [ $counter -lt 5 ]
do
case $counter in
 0) lastdir=0570
   billcycle='EOM_FILES'
   ;;
 1) lastdir=0581
   billcycle='UNE_EOM'
   ;;
 2) lastdir=0582
   billcycle='582_EOM'
   ;;
 3) lastdir=0583
   billcycle='583_EOM'
   ;;
 4) lastdir=0587
   billcycle='UNE_L_EOM'
   ;;
 *) exit 99 ;;
 esac
filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/EOM_reports_'$curdate'/'
remotedir='/fpapps/cfr/CDG/Reports/'$lastdir'/'$billcycle'/'
if [[ -d $filepath ]]
then
 cd $filepath
 transfiles
 for eomfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $eomfnm = *AC0766*.CSV  || $eomfnm = *AC0020*.CSV || $eomfnm = *AC0025*.CSV || $eomfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$eomfnm>$eomfnm.tmp
   mv $eomfnm.tmp $finaldir/$eomfnm
   chmod 775 $eomfnm
   fi
 done
 counter=`expr $counter + 1`
else
 mkdir -p $filepath
 cd $filepath
 transfiles
 for eomfnm in `ls $filepath/AC3.C*.AC0*.D*.T*.CSV*`   ##After files transferred, remove null chars
 do
  if [[ $eomfnm = *AC0766*.CSV  || $eomfnm = *AC0020*.CSV || $eomfnm = *AC0025*.CSV || $eomfnm = *AC0780*.CSV ]] then
   tr '\000' '\ ' <$eomfnm>$eomfnm.tmp
   mv $eomfnm.tmp $finaldir/$eomfnm
   chmod 775 $eomfnm
   fi
 done
 counter=`expr $counter + 1`
files=$(ls $filepath/* 2> /dev/null  wc -l)
 if [ -z $files ]
 then
  cd ..
  rm -r EOM_reports_$curdate
  echo "No files in " $remotedir "\n" >> testfile
  rtncode=6
 fi
fi
done
cd $filepath
chmod -R 777 $filepath
}
###########################################################################
#
#
#      Trigger Fyle generation
#     Written: 01/05/2011 by Howard Pearl
#      For Project Sophia
#     Updated 01/21/2011, changed trigger file location per Raj by Howard Pearl
#
#
############################################################################
function CycleTrigger
{
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
 if [[ -d $triggerpath ]]
 then
  cd $triggerpath
  touch 'CYCLE.'$curdate''$billcycle'.OK'
 else
  mkdir -p $triggerpath
  cd $triggerpath
  touch 'CYCLE.'$curdate''$billcycle'.OK'
 fi
}
function EOMTrigger
{
if [[ $curday -le 15 ]]
then
 month=$curmonth2
 year=$curyear2
 lmonth=`expr $month - 1`
 if test "$lmonth" = "0"
  then
  lmonth=12
  year=`expr $year - 1`
 fi
 curdate=$year$lmonth
 curyear=$year
 #Used for if EOM cycle runs after the end of the month. I.E. December's transfer running after the 31st.
fi
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
 if [[ -d $triggerpath ]]
 then
  cd $triggerpath
  touch 'EOM.'$curdate'.OK'
 else
  mkdir -p $triggerpath
  cd $triggerpath
  touch 'EOM.'$curdate'.OK'
 fi
}
#Also used for weekly trigger files Per Raj (01/17/2011)
function DailyTrigger
{
triggerpath='/fpapps/op/dev/ftproot/ftpdown/TriggerFiles/'
if [[ $curdow != Saturday ]]  #If current day is not Saturday, then the trigger file is created.
 then
  if [[ $curdow != Sunday ]] #If current day is not Sunday, then the trigger file is created.
   then
   if [[ -d $triggerpath ]]
    then
    cd $triggerpath
    touch 'DAILY.'$curdateday'.OK'
   else
    mkdir -p $triggerpath
    cd $triggerpath
    touch 'DAILY.'$curdateday'.OK'
   fi
  fi
 fi
}
#########################################################################
#
#     Used for reseting dates if run for older date
#
#########################################################################
function resetdates
{
  read old_day?"Enter Day as Number: "
 case $old_day in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 case $old_day in
     1) newday=01 ;;
     2) newday=02 ;;
     3) newday=03 ;;
     4) newday=04 ;;
     5) newday=05 ;;
     6) newday=06 ;;
     7) newday=07 ;;
     8) newday=08 ;;
     9) newday=09 ;;
     *) newday=$old_day ;;
     esac
 read old_month?"Enter Month as Number: "
 case $old_month in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 case $old_month in
   1) newmonth=01 ;;
   2) newmonth=02 ;;
   3) newmonth=03 ;;
   4) newmonth=04 ;;
   5) newmonth=05 ;;
   6) newmonth=06 ;;
   7) newmonth=07 ;;
   8) newmonth=08 ;;
   9) newmonth=09 ;;
   01) newmonth=01 ;;
   02) newmonth=02 ;;
   03) newmonth=03 ;;
   04) newmonth=04 ;;
   05) newmonth=05 ;;
   06) newmonth=06 ;;
   07) newmonth=07 ;;
   08) newmonth=08 ;;
   09) newmonth=09 ;;
   10) newmonth=10 ;;
   11) newmonth=11 ;;
   12) newmonth=12 ;;
   *) echo "There are only 12 months in a year"
    exit 99  ;;
   esac
 read old_year?"Enter Year as four digit number: "
 case $old_year in
 +([0-9]) ) print "valid" ;;
 * ) echo "invalid entry, please enter a number"
  exit 99 ;;
 esac
 len=${#old_year}
 if [[ $len -eq 4 ]]
 then
  newyear=$old_year
 else
  echo "Year must be 4 digits"
  exit 99
 fi

 curdate=$old_year''$old_month
 curyear=$old_year
 curdateday=$old_year''$old_month''$old_day
 curmonth=$old_month
 curday=$old_day
}
#########################################################################
#
#
#
#      Main Function Begins Here
#
#
#
#########################################################################
#determines if past date needs to be specified based on command line inputs *see usage note above*
if [[ $2 = 'old' ]]
then
 resetdates
else
 setdates
fi
case $billcycle in
 04th) cycle4 ;;
 06th) cycle6 ;;
 07th) cycle7 ;;
 10th) cycle10 ;;
 13th) cycle13 ;;
 19th) cycle19 ;;
 25th) cycle25 ;;
 Daily) cycleDaily ;;
 EOM) cycleEOM ;;
 Weekly) cycleWeekly ;;
 *) exit 99 ;;
 esac
#if return code is 6, no files available for given cycle.  See system output in Control-M for details as to which files are missing
if [[ $rtncode -eq 6 ]]
then
 cd $finaldir
 cat testfile
 rm -r testfile
 exit 6
else
#Minus one day for 877J transfer
 filepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'$billcycle'/'
 eomfilepath='/fpapps/op/dev/ftproot/ftpdown/'$curyear'_Billing/'$curdate'_Final/'
 day=$curday2
 jmonth=$curmonth2
 jday=`expr $day - 1`
 if [[ $jday -eq 0 ]]
  then
  jmonth=`expr $jmonth - 1`
  if [[ $jmonth -eq 0 ]]
  then
   jmonth=12
  fi
  case $jmonth in
   1) jday=31 ;;
   2) jday=28 ;;
   3) jday=31 ;;
   4) jday=30 ;;
   5) jday=31 ;;
   6) jday=30 ;;
   7) jday=31 ;;
   8) jday=31 ;;
   9) jday=30 ;;
   10) jday=31 ;;
   11) jday=30 ;;
   12) jday=31 ;;
   *) exit 99 ;;
   esac
 fi
 #checks and concats jday between 1 and 9 with a 0 to form correct folders.
 case $jday in
  "1") jday="0"$jday;;
  "2") jday="0"$jday;;
  "3") jday="0"$jday;;
  "4") jday="0"$jday;;
  "5") jday="0"$jday;;
  "6") jday="0"$jday;;
  "7") jday="0"$jday;;
  "8") jday="0"$jday;;
  "9") jday="0"$jday;;
  *) jday=$jday;;
  esac
 acjdate=$jmonth''$jday
#runs 877j trans for cycle files, and 877j and 877a files for monthly files
#Weekly trigger information added 01/17/2011 by Howard Pearl
echo $billcycle
 case $billcycle in
 04th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 06th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 07th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 10th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 13th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 19th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 25th) AC0877jTrans $filepath 'Cycle' $acjdate ;;
 Daily) DailyTrigger ;;
 Weekly) DailyTrigger ;;
 EOM) AC0877aTrans $eomfilepath $acjdate ;;
 *) exit 99 ;;
 esac
 exit 0
fi


Last edited by Don Cragun; 07-29-2013 at 06:39 PM.. Reason: Added ICODE tags
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #378
Difficulty: Medium
AIX was UNIX 03 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545 ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26... (3 Replies)
Discussion started by: deepKrish
3 Replies

2. Shell Programming and Scripting

Output not coming as desired.

Hi guys. I have a file containing some hosts and their IPs. host host1 192.168.2.10 host host2 192.168.2.11 host host3 192.168.2.12 I am writing a script where I want to print these values in 1 line. My script looks like RUNTIME_NODE=`cat hosts.properties | grep host` for i in... (7 Replies)
Discussion started by: Junaid Subhani
7 Replies

3. UNIX for Dummies Questions & Answers

Unable to obtain lock on NFS files

Hello , I am creating a controlfile of database in linux and below is the error coming: SQL> CREATE CONTROLFILE REUSE set DATABASE "newdbcln" RESETLOGS NOARCHIVELOG 2 MAXLOGFILES 5 3 MAXLOGMEMBERS 5 MAXDATAFILES 100 4 5 MAXINSTANCES 1 6 MAXLOGHISTORY... (2 Replies)
Discussion started by: admin_db
2 Replies

4. Shell Programming and Scripting

Help!! needed to get the desired output

Am in need of your help to get the desired output. nameSECURITY.SERVICES.CONFIG:GETVALUEisPrefetchedNsAccessLast2013-09-13 10:50:13 MESTsAccessTotal1sRunningcHitLastnamePUBLIC.SERVER:INVOKEisPrefetchedNsAccessLast2013-09-17 15:02:05... (5 Replies)
Discussion started by: rocky2013
5 Replies

5. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

6. Shell Programming and Scripting

need to get the desired output

Below is the my cide which is working fine but I am not getting the output indesired format.there is some problem in alignment.Can someone help me to correct this? if ]; then summary=$( echo -e "Please review the log file of auto coloclean utility.\n"; echo -e... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

7. Shell Programming and Scripting

SED - output not desired

echo '0x3f 0xfa ae 0xeA' | sed '/0x/ y/abcdef/ABCDEF/' output: 0x3F 0xFA AE 0xEA echo '0x3f 0xfa ae 0xeA' | sed -r '/0x{2}/ y/abcdefg/ABCDEFG/' output: 0x3F 0xFA AE 0xEA my expected output: 0x3F 0xFA ae 0xEA What I want to achieve is change all hexadecimals to UPPER case(only those... (6 Replies)
Discussion started by: kevintse
6 Replies

8. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies

9. Shell Programming and Scripting

script not giving the desired output

Hi, I have a script in which an entry like this ..... FILENAME_B="PIC_${DATE}0732*.JPG" The script connects to an ATM and pull a pic file from it.The format for the file is like PIC_2008061400000001.JPG in the ATM. Means 1st 8 digit is the date(YYYYMMDD) field 2nd 8 digit means hrs... (2 Replies)
Discussion started by: Renjesh
2 Replies

10. Shell Programming and Scripting

Help me in getting the desired output

I wanted to put "|" this sign at starting and at end of every field but its not working with first field like Currently the out put is : abc | abc | abc | xyz | xyz | xyz | But I want the out put in this form: | abc | abc | abc | | xyz | xyz | xyz | plz help me. (2 Replies)
Discussion started by: akash
2 Replies

Featured Tech Videos