File in a folder and loop until time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File in a folder and loop until time
# 1  
Old 05-25-2010
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

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

Moderator's Comments:
Mod Comment I have edited your post to add CODE-tags. Please use CODE-tags when posting code(-fragments), file contents or system output. It is easier to read that way and it helps our automated translation service by excluding parts marked this way from automatic translation. Thank you.
# 2  
Old 05-25-2010
Quote:
Originally Posted by RubinPat
Now this code works except for [...]
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
# 3  
Old 05-25-2010
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....
# 4  
Old 05-25-2010
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.

Code:
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

Last edited by bakunin; 05-26-2010 at 07:23 AM..
# 5  
Old 05-25-2010
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....


Code:
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

Moderator's Comments:
Mod Comment Sigh ... i edited CODE-tags again into your post. I won't do it a third time, but issue infractions instead. If you don't know how to use CODR-tags please refer to the Forums FAQ

Last edited by bakunin; 05-26-2010 at 07:21 AM..
# 6  
Old 05-26-2010
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

Code:
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

Code:
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
# 7  
Old 05-26-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create new folder and text file the same time in one line

Is it possible to create new folder and write a new text file in the folder while the current position is outside the new folder? in one line mkdir folder | echo "hello test"> folder/test.txt not work. (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. UNIX for Dummies Questions & Answers

Loop for file merging in a folder

Dear all, I have a few files in a folder (lets say 5) and each have a few lines in them. I want to create merges of each of them with the other ones e.g. I need the following merges I tried to write a loop and started with combinations of 2, I get the write file names but for each... (17 Replies)
Discussion started by: A-V
17 Replies

3. Shell Programming and Scripting

For loop for number of files in a folder

Hi All, Need a for loop which should run for number of files in a folder and should pass the file name as parameter to another shell script for each loop. Please help me. Thanks. (2 Replies)
Discussion started by: chillblue
2 Replies

4. Shell Programming and Scripting

For loop to read folder which are not under processing

Hi, I want to read folders which do not have a file "processing" in a for loop ordered by timestamp. Currently im doing like this. Like cd /home/working for i in `ls -c1` do some command... done I want to exclude folders which have that "processing" file. The directory... (2 Replies)
Discussion started by: chetan.c
2 Replies

5. Shell Programming and Scripting

loop through files in each folder and perform sed

Dear all, I have few log folders in directory (FILE) and i need to perform sed on each files inside each folders. Here is my script, and i wish to rename each file back to the same file name after modification. Can anyone guide me on my script below? eg: folder1 contain files... (2 Replies)
Discussion started by: ymeyaw
2 Replies

6. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

7. Shell Programming and Scripting

Last modified time of the folder is changing when I view the file inside the directory

Hi., Last modified time of the folder is changing when I view the file inside the directory. Here is the test on sample directory. I believe that ls -l commands gives the time detail w.r.t last modified time. Pl. suggest. bash-3.2$ mkdir test bash-3.2$ cd test bash-3.2$ touch myfile.txt... (2 Replies)
Discussion started by: IND123
2 Replies

8. UNIX for Dummies Questions & Answers

How to append time to folder

Hi i just want to create a folder with current time. For example. foldername_09042010 How do i modify mkdir or write script to do this? (1 Reply)
Discussion started by: pinga123
1 Replies

9. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question