while loop with 3 ifs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop with 3 ifs
# 1  
Old 07-20-2009
while loop with 3 ifs

im messing up somehwere...and can't seem to clean up the script...for it to work

objectives:

1. check for today's file, and sleep 30 secs between retries
2. only allow 5 tries before script should fail.
3. if today's file found, wait 30 seconds for it to process..


code:

Code:
count=0
while [ $count -lt 5 ]
do
    count=`expr $count + 1`
    echo "Try $count"
    RUNDATE=`date "+%h%e"`
    filedate=`ls -l file.xml | awk '{print $6,$7}`
    if [ $count -ge 5 ]
      echo "Too many retries"
      echo "Please check source why the file is not imported"
       exit 0
    else           
     if [ "$filedate" = "$RUNDATE" ] 
     echo "Today's file is ready for processing."
         echo "Sleeping 30 secs to allow it to process "
      sleep 30
           break
     fi
    fi
    if [ "$filedate" != "$RUNDATE" ]
      echo "File date in directory is $filedate and today's date is $RUNDATE. They don't match"
  echo "Sleeping 30 seconds and trying again; 5 tries is my limit"
          sleep 30
    fi
done


Last edited by vgersh99; 07-20-2009 at 05:31 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-20-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 07-21-2009
Can someone assist please? Smilie
# 4  
Old 07-21-2009
Code:
 if [ $count -ge 5 ]; then

is the correct syntax.
# 5  
Old 07-21-2009
Quote:
count=`expr $count + 1`
this is the if - else construct

Code:
if [ $count -eq 2 ]
then
echo "yes"
else
echo "no"
fi

missing ' then '
# 6  
Old 07-21-2009
Btw, if you use sed -x to debug, life would be easy.
# 7  
Old 07-21-2009
Quote:
Originally Posted by sigh2010
Code:
count=0
while [ $count -lt 5 ]
do
    count=`expr $count + 1`
    echo "Try $count"

count will never get greater that 4, first if is not required.

Quote:
Originally Posted by sigh2010
Code:
   RUNDATE=`date "+%h%e"`
    filedate=`ls -l file.xml | awk '{print $6,$7}`

$RUNDATE and $filedate will never match here, check why.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Specifying IFS delimiter in while loop

i have data that is delimited with #x#: file1#x#file2#x#file3 file4#x#file5#x#file6 data is stored in a variable called ALLMYDATA: echo "${ALLMYDATA}" | while IFS="#x#" read -r line junk do echo ${line} done it appears IFS does not allow the specification of more than one... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Remote while IFS

Hello masters of scripting, I've been working to develop some basic monitoring scripts. I have solved one problem, but want to know how to solve the other. I have a script that runs locally to create an output file with the Linux system kernel paramters, preceeded by the system name: ... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

3. Shell Programming and Scripting

Not able to understand IFS

Hi , i am in my initial learning phase of unix. i was going thru the function part. below is the example which was there but i am not able to understand logic and the use of IFS(internal field separator) lspath() { OLDIFS="$IFS" IFS=: for DIR in $PATH ; do echo $DIR ; done IFS="$OLDIFS"... (8 Replies)
Discussion started by: scriptor
8 Replies

4. Shell Programming and Scripting

Bash IFS

I am using bash and resetting IFS as below when reading the command line arguments. I do this so I can call my script as in Ex1. Ex1: ./synt2d-ray3dmod.bash --xsrc=12/20/30 This allows me to split both sides so that when I do "shift" I can get 12/20/30 What I do not understand is... (21 Replies)
Discussion started by: kristinu
21 Replies

5. Shell Programming and Scripting

Nested ifs

hi I keep getting an error with this nested if statement and am getting the error unexpected end of file, can anyone help me as to why this wont execute? #!/bin/bash #script to check wether the -i -v statements run correctly removeFile () { mv $1 $HOME/deleted }... (3 Replies)
Discussion started by: somersetdan
3 Replies

6. Shell Programming and Scripting

While loop and IFS?

Hi, while ; do echo "Please enter " read enter yyyy=${enter:0:4} mm=${enter:5:2} dd=${enter:8:2} result=`validateDate $yyyy $mm $dd` When does the loop keeping repeating till?? till 1 is equal to 1? what does this mean "${enter:0:4}" .The 0 and 4 part?? ... (3 Replies)
Discussion started by: sid22
3 Replies

7. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

8. Shell Programming and Scripting

read and IFS

Hi, This is out of curiosity: I wanted to extract year, month and date from a variable, and thought that combining read and IFS would help, but this doesn't work: echo "2010 10 12" | read y m d I could extract the parts of the date when separated by a -, and setting IFS in a subshell: ... (3 Replies)
Discussion started by: raphinou
3 Replies

9. Shell Programming and Scripting

regarding IFS=

hi i am a learner can some explain "export IFS=$(echo "\n\t\a")" i am not able to understand the functionality please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

10. UNIX for Dummies Questions & Answers

Help on IFS command!

Hi! I am working in korn shell. I want to reset the dimiliter for the set command to "|" but instead of a command prompt return I am getting something as below After issuing the command I am getting this....as if the shell is expecting something else. Can anybody suggest what's the problem. ... (2 Replies)
Discussion started by: udiptya
2 Replies
Login or Register to Ask a Question