Shell script not processing if statement properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan184
    New Member
    • Nov 2006
    • 154

    Shell script not processing if statement properly

    Hi I am trying to create a shell script that will

    look for a contracthead file first and if the contract head file does not exist on day1 exit script.

    Now on day2 if contracthead exists or not run the script uploading files in order
    such as contract line then contract contact

    so the whole order on day1 is
    contracthead
    contractline
    contractcontact

    on day2
    if it exists or not run the script like the order above even if files are missing.
    Now this is the script i came up with.

    basically the if statement for the days block seems not be working
    it works determinig day 1 and 2 but it does nto run the script as i want it to.

    So far.g if it has a contracthead ont he first day and no days file.
    It will upload contract head and go to the else statement and create the days file. It should not do this because the contracthead file exists. If it did not exist then the days file will be created.

    Could you tell me where i am going wrong please.

    Code:
    #!/bin/bash
    # DATE varaible is used to archive the files after ftp
    DATE=`/bin/date +%Y%m%d%H%M`
    # TODIR variable is the target directory for the file on remote system 32x
    TOARCHIVEDIR=/home/tibco/tibco/archive
    # 32x
    TODIR=/home/tibco/tibco/AIS_FTP/test/inbound/Staging
    # FROMDIR variable is the source directory of the file on the local system   5x
    FROMDIR=/home/tibco/ems/FTP/test/inbound/Staging/FTPComplete
    # ARCHDIR variable is the directory file get moved to after tranfer 5x
    ARCHDIR=/home/tibco/archive/testFtp2_32X  
    
    pwd=`pwd`
    echo $pwd
    cd $FROMDIR
    STEXT="The following files were received in: /home/tibco/ems/FTP/test/inbound/Staging/FTPComplete \n"
    ETEXT="The following files were NOT FOUND in: /home/tibco/ems/FTP/test/inbound/Staging/FTPComplete \n"
    
    
    ########Running Loop########
    for file in int_sap_contracthead*.txt 
    int_sap_contractline*.txt
    int_sap_contractcontact_*.txt
    
    do
    	echo $file
    	if [ -f $file ]
    	then
    		#echo " file $file exists"
    		STEXT="%10s $STEXT \n $file "
    		#echo " $STEXT "
    	else
    		ETEXT="%10s $ETEXT \n $file "
    		#echo " $STEXT "
    	fi
    done
    SUCCESS=`printf "$STEXT " `
    FAIL=`printf "$ETEXT " `
    
    
    
    ##################### Checkign if the days file exists######################
    
    NF=Contracthead file not found
    NFEMAIL=Contracthead file not found
    
    if [ -e "days" ]
       then
           echo "*** The days file exists ***"
           rm days
           echo "*** removing the days file ***"
    
    
           echo " ------ This is day 2 - all files are loading ------ "\n\n\n
        ################ FTP ################
    ftp -i -n server <<EOF
    user test 45678
    cd $TODIR
    pwd
    lcd $FROMDIR
    bin
    mput int_sap_contracthead_*.txt
    
    bye
    EOF
    ############  END FTP ###############       
         echo "*** -- UPLOADING FILE CONTRACTHEAD -- ***"
    sleep 8
               elif [ -e "int_sap_contracthead_*.txt" ]
                  then
                 echo "*** -- UPLOADING FTP -- ***"
    ################ FTP ################
    ftp -i -n server <<EOF
    user test 45678
    cd $TODIR
    pwd
    lcd $FROMDIR
    bin
    mput int_sap_contracthead_*.txt
    
    bye
    EOF
    ############  END FTP ###############
                      echo "int_sap_contracthead_*.txt File is uploading"   
               
                      else
                          echo "1" > days
                          echo "Files will not upload today"  
                          exit           
    fi
    
    sleep 8
    ##################### End##################
    
    echo "code still running"
    
    if [ "$file" = "int_sap_contractline*.txt" ]
       then
          ################ FTP ################
    ftp -i -n server <<EOF
    user test 45678
    cd $TODIR
    pwd
    lcd $FROMDIR
    bin
    mput int_sap_contractline_*.txt
    
    bye
    EOF
    ############  END FTP ###############
          echo "contracthead File is uploading"  
    sleep 8
    else 
    echo "there is NO contractcontact File to upload"  
    fi
    
    
    if [ "$file" = "int_sap_contractcontact*.txt" ]
       then
           ################ FTP ################
    ftp -i -n server <<EOF
    user test 45678
    cd $TODIR
    pwd
    lcd $FROMDIR
    bin
    mput int_sap_contractcontact_*.txt
    
    bye
    EOF
    ############  END FTP ############### 
          echo "contractcontact File is uploading"   
    
    else echo "there is NO contractcontact File to upload"
    fi
    
    
    
    HEADER="FTP Summary of the files "
    	
    mail -s "$HEADER" [email protected] <<EOF
    $SUCCESS
    	$FAIL
    EOF
    
    # create the archive directory based on timestamp and move files
    cd $ARCHDIR
    mkdir $DATE
    mv $FROMDIR/int_sap_contractline_*.txt $ARCHDIR/$DATE/
    mv $FROMDIR/int_sap_contractcontact $ARCHDIR/$DATE/
    mv $FROMDIR/int_sap_contracthead_*.txt $ARCHDIR/$DATE/
    exit
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    As far as I can tell, you have the script set up to upload the file in the case that days does not exist and the contactheader does exist.

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      Hi Jonathan,

      I'm not sure how well I understand your problem. In general, I'd say that it is a good idea to post somewhat cut down versions of a script so that the specific area where it's going wrong stands out better.

      The loop on lines 23-34 seems odd. For each file, it adds 10 spaces to the beginning of the string and then adds the filename on a new line at the end. Did you really want the 10 spaces per file?

      Also, the lines
      Code:
      NF=Contracthead file not found
      NFEMAIL=Contracthead file not found
      don't appear to be valid. If I put those onto a bash command line, I get:
      Code:
      [prn@deimos ~]$ NF=Contracthead file not found
      not:   ERROR: cannot open `not' (No such file or directory)
      found: ERROR: cannot open `found' (No such file or directory)
      Did you intend those to define strings? If so, they should be in quotes. OTOH, you never use them, so why are they there?

      Then, we get to the point that (I think) you are asking about.

      You said
      if it has a contracthead ont he first day and no days file.
      It will upload contract head and go to the else statement and create the days file. It should not do this because the contracthead file exists. If it did not exist then the days file will be created.
      I take that to mean that if there is a contracthead, but no days file, then the script should upload the contracthead file and NOT create the days file. It should create the days file ONLY IF the contracthead file does not exist. Right? At least, that appears to be what the script says.

      I read the script as follows:

      The script checks for the existence of a file called days. If it finds days, it removes the file and goes directly to ftp to upload all the int_sap_contrac thead*.txt files.

      If it does not find days, then it should check for the existence of int_sap_contrac thead*.txt files and upload them if it finds them, but NOT create days.

      In either of the first two cases, it should then drop out of the if conditional at the fi (line 88) and upload any contractline and contractcontrac t files that exist.

      If it does not find days AND there are no int_sap_contrac thead*.txt files then it should create days and exit.

      Is that what it's supposed to do?

      As I understand it, what it is doing is finding a contracthead file and NO days file, but uploading contracthead AND creating days. Right?

      Now, that's not the result I was getting with a simplified version of your script. What I was finding was that with no days file, it always ignored the contracthead file, created b]days[/b] and would then exit.

      I find that the conditional:
      Code:
      elif [ -e "int_sap_contracthead_*.txt" ]
      fails every time. You need
      Code:
      elif [ -e int_sap_contracthead_*.txt ]
      (without the quotes) in order to test a file glob. With the quotes, it appears to be looking for a file with a literal asterisk in its name, which it will not find.

      Here's a simplified version of your script. Try this for the logic and then fill in the ftp and so forth.

      Code:
      #! /bin/bash
      
      ########## Checkign if the days file exists ##########
      
      if [ -e "days" ]
         then
             echo "*** The days file exists ***"
             rm days
             echo "*** removing the days file ***"
             echo " ------ This is day 2 - all files are loading ------ "
             echo "*** -- UPLOADING FILE CONTRACTHEAD -- ***"
      elif [ -e int_sap_contracthead_*.txt ]
         then
             echo "*** -- UPLOADING CONTRACTHEAD (without \"days\") -- ***"
      else
             echo "1" > days
             echo "Files will not upload today"
             exit
      fi
      
      echo "code still running"
      exit
      I've cut this down by removing all the substance from your script, leaving just the structure of the "if" block. Try it.

      Best Regards,
      Paul

      Comment

      Working...