How to get script to wait until status is true before continuing?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get script to wait until status is true before continuing?
# 1  
Old 05-14-2013
How to get script to wait until status is true before continuing?

I'm extremely new to shell scripting so I apologize for the crudeness of my descriptions. I am editing a script that will write files (e.g. Job0_A.com, Job1_A.com, etc.) and then run them through a program called gaussian (computational chemistry program). The script will then take the output files (e.g. Job0_A.log, Job1_A.log, etc.) and run them through a fortran program to do further optimizations. Since I am on a multi-user cluster, I have edited the script to run batch files. It seems to have worked but there is a problem. The script will not wait until the .log file has been created (I am running large systems so it make take several hours for the .log to be created) and will just go on to the next job. How do I make the script wait until
Code:
ls | grep 'Job'$num'_A.log >& /dev/null

is true? Also, when I run the script I get a file called $ with nothing in it. I searched the script to see if I made a mistake that would cause this but I can't find anything.
# 2  
Old 05-14-2013
One easy way is to have the first process create the file in a different path (name, extension, directory) of the same device, and when done and happy, move it so the next process can start. Usually, scripts start the next process right then, so it is not an issue. If the second process needs multiple processes from the first set of processes, it waits for one file and then waits for the next until all are present. Serial data can even be piped from one program to the next without a flat file, or with the flat file produce by tee on the pipeline. However, scientific array problems want to mmap() the whole file into VM for processing, so pipes need not apply!
Code:
#!/bin/bash
 
while :
do
 if [ -f file1 -a -f file2 -a -f file3 ]
 then
  break
 fi
 
 sleep 1
done
 
process2 file1 file2 file3

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 05-14-2013
Thanks for the reply! Again, I'm very new to this so I apologize, but I don't understand some of the code. In
Code:
if [ -f file1 -a -f file2 -a -f file3 ]

what does the -f and -a mean? Also is the "1" after sleep the number of seconds until the loop restarts?
I only need two files from the first process and they must both be present at the same time for the next process to work. Do you think this would work:
Code:
ls | grep 'file1' 'file2' >& /dev/null
while:
do 
    if ( $status == 0 )
    then
       break
    fi
    sleep 1
done

process2 file1 file2

Again, I thank you for your help!
# 4  
Old 05-14-2013
That 'if' looks a lot like C shell, what shell are you using?
# 5  
Old 05-14-2013
-f is file from man test, as '/bin/[' is a hard link to '/bin/test' and some shells mimic it with builtins.
# 6  
Old 05-14-2013
Okay thanks! I'll give it a try.

It is the C shell. The script I am editing has /bin/csh -f.

Last edited by butson; 05-14-2013 at 06:48 PM.. Reason: wrong info.
# 7  
Old 05-14-2013
That would have been nice to know. The code you've been given so far will not work in C shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File validation prior to continuing script

Hi Guys, I am trying to find a way within a bash script to check a file that exists in the same directory to ensure every line starts with 44 and is 12 digits long. If it doesn't then print some sort of text advising of the error and stop the script from going any further. If all lines... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

Script using Wait

Hi, written a script which uses wait as follows Main.sh #!/usr/bin/ksh nohup scrpit1 1 & pid_1=$! nohup scrpit1 2 & pid_2=$! wait $pid_1 wait $pid_2 nohup scrpit1 3 & pid_1=$! nohup scrpit1 4 & (1 Reply)
Discussion started by: krux_rap
1 Replies

3. Programming

Wait status

hi all! In my C++ program I have a parent process which forks 5 children processes.The processes do a job and then they have to do some sort of sleeping(not terminate) until the parent wakes them up again.There might be 1,2,5 or even 0 processes awake at any moment.The thing is that in the... (9 Replies)
Discussion started by: vlm
9 Replies

4. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

5. Shell Programming and Scripting

Trapping exit and continuing

Hello I need to source a script. But that script terminates with a trailing exit. Which exits my script. I'm using bash, and this doesn't work: trap 'echo disabled' EXIT source other_file trap '' EXIT Instead, it calls my trap, but then exits anyway. I could get disgusting and... (4 Replies)
Discussion started by: brsett
4 Replies

6. Shell Programming and Scripting

check/wait for files to exist before continuing

I'm attempting to write a pretty simple script. It opens a Filemaker file successfully. That Filemaker file takes around 30-90 seconds to finish. When it's done, it writes a few .xml files into the same directory where my shell script and the Filemaker script reside. In my script, how can I... (2 Replies)
Discussion started by: alternapop
2 Replies

7. Shell Programming and Scripting

make sure logged in as userx before continuing script

i have a bash script and I want to add to the begining of the script to make sure that the script is being ran as you are logged in as a certain user (userx) before continuing to run the script....how? (2 Replies)
Discussion started by: ajp7701
2 Replies

8. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

why isn't the exit status true?

the code: do } ] || mkdir -p ${mk_backup_dir} && echo "ERROR: release backup directory creation failed -${mk_backup_dir}" && exit done echo "INFO: Backup directories created" the result: mkdir: "/cm/uat_releases/riab/uat/2345": Permission denied ERROR: release backup directory... (5 Replies)
Discussion started by: mjays
5 Replies
Login or Register to Ask a Question