Sponsored Content
Top Forums Shell Programming and Scripting Unable to obtain the desired output Post 302837371 by Don Cragun on Thursday 25th of July 2013 03:40:29 PM
Old 07-25-2013
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).
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 03:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy