473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Shell script not processing if statement properly

154 100+
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.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. # DATE varaible is used to archive the files after ftp
  3. DATE=`/bin/date +%Y%m%d%H%M`
  4. # TODIR variable is the target directory for the file on remote system 32x
  5. TOARCHIVEDIR=/home/tibco/tibco/archive
  6. # 32x
  7. TODIR=/home/tibco/tibco/AIS_FTP/test/inbound/Staging
  8. # FROMDIR variable is the source directory of the file on the local system   5x
  9. FROMDIR=/home/tibco/ems/FTP/test/inbound/Staging/FTPComplete
  10. # ARCHDIR variable is the directory file get moved to after tranfer 5x
  11. ARCHDIR=/home/tibco/archive/testFtp2_32X  
  12.  
  13. pwd=`pwd`
  14. echo $pwd
  15. cd $FROMDIR
  16. STEXT="The following files were received in: /home/tibco/ems/FTP/test/inbound/Staging/FTPComplete \n"
  17. ETEXT="The following files were NOT FOUND in: /home/tibco/ems/FTP/test/inbound/Staging/FTPComplete \n"
  18.  
  19.  
  20. ########Running Loop########
  21. for file in int_sap_contracthead*.txt 
  22. int_sap_contractline*.txt
  23. int_sap_contractcontact_*.txt
  24.  
  25. do
  26.     echo $file
  27.     if [ -f $file ]
  28.     then
  29.         #echo " file $file exists"
  30.         STEXT="%10s $STEXT \n $file "
  31.         #echo " $STEXT "
  32.     else
  33.         ETEXT="%10s $ETEXT \n $file "
  34.         #echo " $STEXT "
  35.     fi
  36. done
  37. SUCCESS=`printf "$STEXT " `
  38. FAIL=`printf "$ETEXT " `
  39.  
  40.  
  41.  
  42. ##################### Checkign if the days file exists######################
  43.  
  44. NF=Contracthead file not found
  45. NFEMAIL=Contracthead file not found
  46.  
  47. if [ -e "days" ]
  48.    then
  49.        echo "*** The days file exists ***"
  50.        rm days
  51.        echo "*** removing the days file ***"
  52.  
  53.  
  54.        echo " ------ This is day 2 - all files are loading ------ "\n\n\n
  55.     ################ FTP ################
  56. ftp -i -n server <<EOF
  57. user test 45678
  58. cd $TODIR
  59. pwd
  60. lcd $FROMDIR
  61. bin
  62. mput int_sap_contracthead_*.txt
  63.  
  64. bye
  65. EOF
  66. ############  END FTP ###############       
  67.      echo "*** -- UPLOADING FILE CONTRACTHEAD -- ***"
  68. sleep 8
  69.            elif [ -e "int_sap_contracthead_*.txt" ]
  70.               then
  71.              echo "*** -- UPLOADING FTP -- ***"
  72. ################ FTP ################
  73. ftp -i -n server <<EOF
  74. user test 45678
  75. cd $TODIR
  76. pwd
  77. lcd $FROMDIR
  78. bin
  79. mput int_sap_contracthead_*.txt
  80.  
  81. bye
  82. EOF
  83. ############  END FTP ###############
  84.                   echo "int_sap_contracthead_*.txt File is uploading"   
  85.  
  86.                   else
  87.                       echo "1" > days
  88.                       echo "Files will not upload today"  
  89.                       exit           
  90. fi
  91.  
  92. sleep 8
  93. ##################### End##################
  94.  
  95. echo "code still running"
  96.  
  97. if [ "$file" = "int_sap_contractline*.txt" ]
  98.    then
  99.       ################ FTP ################
  100. ftp -i -n server <<EOF
  101. user test 45678
  102. cd $TODIR
  103. pwd
  104. lcd $FROMDIR
  105. bin
  106. mput int_sap_contractline_*.txt
  107.  
  108. bye
  109. EOF
  110. ############  END FTP ###############
  111.       echo "contracthead File is uploading"  
  112. sleep 8
  113. else 
  114. echo "there is NO contractcontact File to upload"  
  115. fi
  116.  
  117.  
  118. if [ "$file" = "int_sap_contractcontact*.txt" ]
  119.    then
  120.        ################ FTP ################
  121. ftp -i -n server <<EOF
  122. user test 45678
  123. cd $TODIR
  124. pwd
  125. lcd $FROMDIR
  126. bin
  127. mput int_sap_contractcontact_*.txt
  128.  
  129. bye
  130. EOF
  131. ############  END FTP ############### 
  132.       echo "contractcontact File is uploading"   
  133.  
  134. else echo "there is NO contractcontact File to upload"
  135. fi
  136.  
  137.  
  138.  
  139. HEADER="FTP Summary of the files "
  140.  
  141. mail -s "$HEADER" test@domain.com <<EOF
  142. $SUCCESS
  143.     $FAIL
  144. EOF
  145.  
  146. # create the archive directory based on timestamp and move files
  147. cd $ARCHDIR
  148. mkdir $DATE
  149. mv $FROMDIR/int_sap_contractline_*.txt $ARCHDIR/$DATE/
  150. mv $FROMDIR/int_sap_contractcontact $ARCHDIR/$DATE/
  151. mv $FROMDIR/int_sap_contracthead_*.txt $ARCHDIR/$DATE/
  152. exit
May 7 '07 #1
2 6580
Motoma
3,237 Expert 2GB
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.
May 9 '07 #2
prn
254 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. NF=Contracthead file not found
  2. NFEMAIL=Contracthead file not found
  3.  
don't appear to be valid. If I put those onto a bash command line, I get:
Expand|Select|Wrap|Line Numbers
  1. [prn@deimos ~]$ NF=Contracthead file not found
  2. not:   ERROR: cannot open `not' (No such file or directory)
  3. found: ERROR: cannot open `found' (No such file or directory)
  4.  
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_contracthead*.txt files.

If it does not find days, then it should check for the existence of int_sap_contracthead*.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 contractcontract files that exist.

If it does not find days AND there are no int_sap_contracthead*.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:
Expand|Select|Wrap|Line Numbers
  1. elif [ -e "int_sap_contracthead_*.txt" ]
fails every time. You need
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. #! /bin/bash
  2.  
  3. ########## Checkign if the days file exists ##########
  4.  
  5. if [ -e "days" ]
  6.    then
  7.        echo "*** The days file exists ***"
  8.        rm days
  9.        echo "*** removing the days file ***"
  10.        echo " ------ This is day 2 - all files are loading ------ "
  11.        echo "*** -- UPLOADING FILE CONTRACTHEAD -- ***"
  12. elif [ -e int_sap_contracthead_*.txt ]
  13.    then
  14.        echo "*** -- UPLOADING CONTRACTHEAD (without \"days\") -- ***"
  15. else
  16.        echo "1" > days
  17.        echo "Files will not upload today"
  18.        exit
  19. fi
  20.  
  21. echo "code still running"
  22. exit
  23.  
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
May 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Clay Beatty | last post by:
When you create database diagrams in Enterprise Manager, the details for constructing those diagrams is saved into the dtproperties table. This table includes an image field which contains most of...
4
by: mailar | last post by:
Hi, I am trying to register my UDF named 'myUDF.sql" (which has @ as its command terminating character) with sample database I am able to do so through the Linux shell prompt using the following...
1
by: Rob | last post by:
I'm running a batch file using the Shell function. When I manually launch the batch file, the window remains open, since I use the 'pause' statement. But when I launch the batch file within...
2
by: Curtiosity | last post by:
I have done a create or replace view called creditcard1. If I do a "select * from creditcard1" it retrieves the data just fine. If I try to do a statement where I am listing the column names it...
4
by: gwise | last post by:
We're running DB2 v8.2 (fix pak 11) on Red Hat Linux Enterprise 3 (kernel 2.4.21-32) on Itanium (IA64). For a few months now, we've had a recurring problem where a shell script that we run nightly...
9
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell...
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
3
by: xtremebass | last post by:
Hi , I am want to run the shell script to run every 10 minutes of interval ,so that i have used crontab command for scheduling the shell script .. it cant works . i have checked wall command in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.