File in a folder and loop until time

Hi All,

I am stuck with this requirement. My requirement is that a job will run daily at anytime before and would look for a file named filename.txt in a folder and mv to a different folder and if not found until 9 A.M it will exit with a success code.

Now I did the follwing ..but there is an issue I am having

wait time=1
current time=$(date +'%H%M')
max-time 5
 
run time =0
while:
do
cd mydir
if [[current time < '900']]
then if [-e filename.txt]
then mv folder1  folder2
exit 0;
fi
else
exit 0;
fi
 
if [run time -ge max time] then echo "time out"
exit 0;
fi
 
sleep 60
run time=run time + wait time
done

Now this code works except for one instance when the time passes 9 A.M and the filename.txt doesnot arrive...it keeps looping...i need it to exit..

My code above is a rough one...but mostly it....can you please tell me how to work it out or if there is any other way....

Sorry, but the code you posted could never have run. It is crammed full with syntactical errors, which would make it abort immediately. Please post your real code in order to find out what is not working.

Furthermore: this looks suspiciously like homework, for which a separate part of the forum is designated. Please state if this is homework.

bakunin

Hi Bakunin please take my words this is not a homework and neither I am an unix guy...its just that alongwith some different application i am using this unix application for my project....also i mentioned about the code as a part to explain what I did...i dont have the code handy.....please dont run the code....i just wanted to convey that this is what i did and the problem that I am getting...there is some poblem in my logic when i am coding and thats where i am getting stuck...plz let me know if my explanation answers your question....

To be honest it eludes me what "max_time", "run_time" and "wait_time" do. For your requirement as stated they are completely unnecessary. I have corrected the most obvious syntactic errors but i doubt that this script will do anything useful.

Further, you haven't stated which shell (and, hence, shell programming language) you use, which operating system (this might make a difference on tools like "date") and you still haven't provided the script you actually wrote, but some abstracted thing which never could have run.

My magic crystal globe by which i read minds, correct things i haven't seen yet and generally do wonders is in repair right now, so the bad news is: as long as you are unwilling to give some data to work with i am unable (and unwilling) to help you.

You might want to read How to ask questions the smart way and do accordingly.

current_time=$(date +'%H%M')
typeset -i max_time=5
typeset -i run_time=0
typeset -i wait_time=1

while : ; do
     current_time=$(date +'%H%M')
     if [ $current time -gt 900 ] ; then
          if [ -e "/mydir/filename.txt" ] ; then
               mv /mydir/folder1 /mydir/folder2
               exit 0
          fi
     else
          exit 0
     fi
 
     if [ $run_time -ge $max_time ] ; then
          echo "time out"
          exit 0
     fi
 
     sleep 60
     (( run_time += wait_time ))
done

I hope this helps.

bakunin

the development envirnement is AIX Unix system....it requires kornshell I suppose....becuase i didn't use bash or c shell....now about he script....please find it below and my apologies for the trouble...current time, max time and wait time are variables I declared....and then I tried using it in the script...but the problem I am facing is when time is after 9 AM the script exits...which is fine...when it is before 9 AM it goes and looks for the file and if it finds it it exits.. which is also fine..and if it doesnot find it loops....it is fine ....but this looping actually keeps continuing if it doesn't find the file even after 9 AM...it only exits after the total max time....it is strange...than within the loop it doesn't recognise the current time...my requirement would be the loop should continue and would exit as soon as it is 9 AM...but it doesn't work the same ...i hope i could explain.....I developed it from some other existing codes....but it would be helpful if you have some other way out.....I have no clue where to fix and if there is some other way out....

current_time=$(date +'%H%M') [to get current time]
max_time=5 {used for how long the file would be watched with loop]
wait_time=1 (to add to runtime}

run_time=0
while :  
do
     if [ $current time < '900' ] ; then
          if [ -e "/mydir/filename.txt" ] ; then
               mv /mydir/folder1 /mydir/folder2
               exit 0
          fi
     else
          exit 0
     fi
 
     if [ $run_time -ge $max_time ] ; then
          echo "time out"
          exit 0
     fi
 
     sleep 60
     (( $run_time = $run_time+$wait_time ))
done

Well, "$current_time" is set once (upon start of the script) and then never changes - but after waiting some time "$current_time" doesn't hold the current time any more, but the time at the start of the script. It should be updated with every execution in the loop, though. This is why i have put in the line

current_time=$(date +'%H%M')

as first command within the while-loop, which you have subsequently removed for no known reason. (compare my code and your version of it).

Second, the script will run 5 minutes max, because it terminates whenever "$run_time" reaches the value of "$max_time". "$run_time" is incremented with every execution of the loop and with the fifth execution the line

if [ $run_time -ge $max_time ] ; then

will evaluate to TRUE and the script will echo "Time out" and exit. If this is desired behavior or not i don't know, but your requirement says nothing about it.

I hope this helps.

bakunin

Thanks Bakunin....for your consistent help....I honestly appreciate it....Now about the
current_time=$(date +'%H%M') ...I saw that in your code being mentioned but removed it because I was tellling you my code ..what I had in hand....I will try your code today with the current_time as you said....yes you are right actually about the run_time....it was already there in another code as i said before and I thought to keep it....My requirement is simply :A job will run daily at anytime before 9 AM and would look for a file named filename.txt in a folder and if it finds then move it to a different folder and if not found until 9 A.M it will exit with a success code. Can you suggest me a code please. Lots of thanks in advance.

Ok, i have commented the code as much as possible to make it obvious what it does.

#!/bin/ksh

typeset -i iTime=$(date +'%H%M)             # current time
typeset    fFile="/some/file/to/watch"      # file to observe
typeset    fTgt="/some/other/directory"     # target location to put it
typeset -i iInterval=60                     # interval in seconds

while [ $iTime -lt 900 ] ; do               # loop until it is 9:00 am
     if [ -e "$fFile" ] ; then              # if file is found, move it, then exit
          mv "$fFile" "$Tgt"
          print - "File $fFile moved, exiting"
          exit 1
     fi

     sleep $iInterval                       # if file is not found, sleep
     iTime=$(date +'%H%M')                  # update current time
done
                                            # to get here means we never
                                            # encountered the file (otherwise we
                                            # would have left via the "exit 1" above)
                                            # therefore the time must have been up
print - "File $fFile not found but time is up, exiting"
exit 0

I hope this helps.

bakunin

Thanks Bakunin, my own code with the current time inside worked as per my requirement
and also your code I tried and found it better and it also worked.
I need a last suggestion from you ...i just came to know that if the job starts after 9 and
finds the file in the folder
then also it needs to be moved or else it exits and if it starts
before 9 it should look for the file until 9 and then if it finds its ok or else it should exit at 9....
---To be precise
The job starts looks for the file before or after 9
if it finds it moves the file from source to target and exits
If it doesnot find the file and the time is after 9 then it exits
But if it doesnot find the file it and the time is before 9 then looks for the file until 9 and exits

Just that your help is awesome.Thanks

---------- Post updated at 06:43 AM ---------- Previous update was at 06:11 AM ----------

I got it myself...But thanks a ton Bukanin.